【问题标题】:graphql type error property "some" undefinedgraphql 类型错误属性“一些”未定义
【发布时间】:2021-05-08 03:12:36
【问题描述】:

我是开始一个 graphql 项目的初学者我已经创建了 index.js 文件,我在阅读文档后编写了一些代码这里是我的代码

import express from 'express';
const { ApolloServer } = require('apollo-server');
import resolvers from './resolver'
const typeDefs = require('./schema');
const app = express();

const server = new ApolloServer({ typeDefs ,resolvers});
server.applyMiddleware({ app, path: '/graphql' });
app.listen(8080);

这是我的 schema.js 代码

const { gql } = require('apollo-server');

const typeDefs = gql `
type Query{
    hi:String
}`
module.export =typeDefs

这是我的解析器文件

export default{
    Query:{
        hi:(parent,args,context,info) =>'hi'
    },
}

【问题讨论】:

  • 跟随一些教程和工作示例/repo

标签: node.js express graphql apollo-server


【解决方案1】:

您应该使用apollo-server-express 包,以便您可以使用applyMiddleware 方法。

例如

import express from 'express';
import { ApolloServer, gql } from 'apollo-server-express';
const app = express();

const typeDefs = gql`
  type Query {
    hi: String
  }
`;

const resolvers = {
  Query: {
    hi: () => 'hi',
  },
};

const server = new ApolloServer({ typeDefs, resolvers });
server.applyMiddleware({ app, path: '/graphql' });
app.listen(8080, () => console.log('Apollo server started at http://localhost:8080'));

输出:

 ⚡  curl -i -H 'Content-Type: application/json' -X POST -d '{"query": "query {hi}"}' http://localhost:8080/graphql
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 21
ETag: W/"15-3MWwR0/DgM8B4VCmB0O7NSgG144"
Date: Thu, 04 Feb 2021 02:51:29 GMT
Connection: keep-alive
Keep-Alive: timeout=5

{"data":{"hi":"hi"}}

【讨论】:

    猜你喜欢
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2020-08-29
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    相关资源
    最近更新 更多