无法让 Graphql 工具读取我的 schema.graphql 文件
Can't get Graphql-tools to read my schema.graphql file
我正在使用 Apollo-server-express 和 Graphql-tools。我遍历了 Graphql-tools 文档,但我无法让它工作。我正在尝试将我的 schema.graphql 文件导入为我的 typeDef。似乎 Graphql-tools 应该可以让这一切变得简单,但有些东西还没有落到实处。
index.js
const { ApolloServer } = require("apollo-server-express");
const { makeExecutableSchema } = require('@graphql-tools/schema');
const express = require("express");
const { join } = require("path");
const { loadSchema } = require("@graphql-tools/load");
const { GraphQLFileLoader } = require("@graphql-tools/graphql-file-loader");
const { addResolversToSchema } = require("@graphql-tools/schema");
const app = express();
const resolvers = {
Query: {
items: (parent, args, ctx, info) => {
return ctx.prisma.item.findMany();
},
},
Mutation: {
makeItem: (parent, args, context, info) => {
const newItem = context.prisma.item.create({
data: {
...args,
price: parseInt(Math.ceil(args.price * 100)),
},
});
return newItem;
},
deleteItem: (parent, args, context, info) => {
return context.prisma.item.delete({
where: {
id: args.id,
},
});
},
},
};
const schemaSource = loadSchemaSync(join(__dirname, "schema.graphql"), {
loaders: [new GraphQLFileLoader()],
});
const schema = makeExecutableSchema({
typeDefs: schemaSource,
resolvers,
});
const server = new ApolloServer({
schema,
resolvers,
});
server.applyMiddleware({ app });
app.listen(
{ port: 4000 },
() =>
console.log(
` => Backend server is now running on port http://localhost:4000`
)
);
schema.graphql
type Query {
items: [Item!]!
}
type Mutation {
makeItem(
piece: String!
image: String!
identifier: String!
price: Float!
itemNumber: Int!
): Item!
deleteItem(id: ID!): Item!
}
type Item {
id: ID!
piece: String!
image: String!
identifier: String!
price: Int!
itemNumber: Int!
}
在当前状态下,我收到一条错误消息:“错误:typeDefs 必须是字符串、数组或架构 AST,已获取对象”
据我了解,makeExecutableSchema 应该执行所有必要的步骤,例如将架构更改为字符串。我似乎无法弄清楚这里发生了什么,我们将不胜感激。
loadSchemaSync
将使用提供的指针加载 GraphQLSchema
对象。您应该改用 loadTypedefsSync
。
const sources = loadTypedefsSync(join(__dirname, "schema.graphql"), {
loaders: [new GraphQLFileLoader()],
});
const typeDefs = sources.map(source => source.document)
const server = new ApolloServer({ typeDefs, resolvers })
如果您想使用 loadSchema
,则不需要使用 makeExecutableSchema
,因为您的架构已经创建。所以你会这样做,而不是如 docs:
中所示
const schema = loadSchemaSync(join(__dirname, "schema.graphql"), {
loaders: [new GraphQLFileLoader()],
});
const resolvers = {...};
const schemaWithResolvers = addResolversToSchema({
schema,
resolvers,
});
const server = new ApolloServer({ schema: schemaWithResolvers })
我正在使用 Apollo-server-express 和 Graphql-tools。我遍历了 Graphql-tools 文档,但我无法让它工作。我正在尝试将我的 schema.graphql 文件导入为我的 typeDef。似乎 Graphql-tools 应该可以让这一切变得简单,但有些东西还没有落到实处。
index.js
const { ApolloServer } = require("apollo-server-express");
const { makeExecutableSchema } = require('@graphql-tools/schema');
const express = require("express");
const { join } = require("path");
const { loadSchema } = require("@graphql-tools/load");
const { GraphQLFileLoader } = require("@graphql-tools/graphql-file-loader");
const { addResolversToSchema } = require("@graphql-tools/schema");
const app = express();
const resolvers = {
Query: {
items: (parent, args, ctx, info) => {
return ctx.prisma.item.findMany();
},
},
Mutation: {
makeItem: (parent, args, context, info) => {
const newItem = context.prisma.item.create({
data: {
...args,
price: parseInt(Math.ceil(args.price * 100)),
},
});
return newItem;
},
deleteItem: (parent, args, context, info) => {
return context.prisma.item.delete({
where: {
id: args.id,
},
});
},
},
};
const schemaSource = loadSchemaSync(join(__dirname, "schema.graphql"), {
loaders: [new GraphQLFileLoader()],
});
const schema = makeExecutableSchema({
typeDefs: schemaSource,
resolvers,
});
const server = new ApolloServer({
schema,
resolvers,
});
server.applyMiddleware({ app });
app.listen(
{ port: 4000 },
() =>
console.log(
` => Backend server is now running on port http://localhost:4000`
)
);
schema.graphql
type Query {
items: [Item!]!
}
type Mutation {
makeItem(
piece: String!
image: String!
identifier: String!
price: Float!
itemNumber: Int!
): Item!
deleteItem(id: ID!): Item!
}
type Item {
id: ID!
piece: String!
image: String!
identifier: String!
price: Int!
itemNumber: Int!
}
在当前状态下,我收到一条错误消息:“错误:typeDefs 必须是字符串、数组或架构 AST,已获取对象”
据我了解,makeExecutableSchema 应该执行所有必要的步骤,例如将架构更改为字符串。我似乎无法弄清楚这里发生了什么,我们将不胜感激。
loadSchemaSync
将使用提供的指针加载 GraphQLSchema
对象。您应该改用 loadTypedefsSync
。
const sources = loadTypedefsSync(join(__dirname, "schema.graphql"), {
loaders: [new GraphQLFileLoader()],
});
const typeDefs = sources.map(source => source.document)
const server = new ApolloServer({ typeDefs, resolvers })
如果您想使用 loadSchema
,则不需要使用 makeExecutableSchema
,因为您的架构已经创建。所以你会这样做,而不是如 docs:
const schema = loadSchemaSync(join(__dirname, "schema.graphql"), {
loaders: [new GraphQLFileLoader()],
});
const resolvers = {...};
const schemaWithResolvers = addResolversToSchema({
schema,
resolvers,
});
const server = new ApolloServer({ schema: schemaWithResolvers })