GraphQLJS 使用 .graphql 文件从 nodejs 进行查询
GraphQLJS use a .graphql file for a query from nodejs
我已经创建了一个基本的 GraphQL Express 应用程序,我想将来自预定义查询的预定义数据与特定路由捆绑在一起。
理想情况下,查询应该允许提供参数以便灵活使用,我希望能够将查询保存到文件中并 运行 根据需要提供参数,但提供特定于当前所需数据的参数.
我可以使用以下查询
查询api
query authors(ids: [1337, 42]) {
name,
id
}
query.graphql
文件应类似于以下内容:
getAuthorsById($ids: Int[]) {
authors(ids: $ids) {
name,
id
}
}
我想在 Node 服务器中做的是从 query.graphql
文件中获取内容并在触发特定路由时执行它,例如
const query = somehowImportTheQuery('./query.graphql')
graphql(schema, query([1337, 42]))
上面的代码 somehowImportTheQuery
应该导入查询和 return 一个可以用参数调用的函数 getAuthorsById
。
这样的东西已经存在了吗?或者是否有任何工具或文档可以帮助我实现所需的功能?
感谢您的帮助!
您可以使用 graphql-tools
模块的 documents-loading 从不同来源加载 GraphQL 操作文档。
例如
index.ts
:
import { GraphQLSchema, buildSchema, graphql } from 'graphql';
import { loadDocumentsSync, GraphQLFileLoader } from 'graphql-tools';
import path from 'path';
const typeDefs: string = `
type Author {
id: ID!
name: String
}
type Query {
authors(ids: [ID]!): [Author]!
}
`;
const resolvers = {
authors({ ids }) {
return [
{ id: ids[0], name: 'a' },
{ id: ids[1], name: 'b' },
];
},
};
const schema: GraphQLSchema = buildSchema(typeDefs);
const query = loadDocumentsSync(path.resolve(__dirname, './query.graphql'), {
loaders: [new GraphQLFileLoader()],
});
graphql({
schema,
source: query[0].rawSDL!,
rootValue: resolvers,
variableValues: { ids: [1337, 42] },
}).then(({ data }) => {
console.log(data);
});
query.graphql
:
query getAuthorsById($ids: [ID]!) {
authors(ids: $ids) {
name
id
}
}
执行结果:
[Object: null prototype] {
authors:
[ [Object: null prototype] { name: 'a', id: '1337' },
[Object: null prototype] { name: 'b', id: '42' } ] }
我已经创建了一个基本的 GraphQL Express 应用程序,我想将来自预定义查询的预定义数据与特定路由捆绑在一起。
理想情况下,查询应该允许提供参数以便灵活使用,我希望能够将查询保存到文件中并 运行 根据需要提供参数,但提供特定于当前所需数据的参数.
我可以使用以下查询
查询apiquery authors(ids: [1337, 42]) {
name,
id
}
query.graphql
文件应类似于以下内容:
getAuthorsById($ids: Int[]) {
authors(ids: $ids) {
name,
id
}
}
我想在 Node 服务器中做的是从 query.graphql
文件中获取内容并在触发特定路由时执行它,例如
const query = somehowImportTheQuery('./query.graphql')
graphql(schema, query([1337, 42]))
上面的代码 somehowImportTheQuery
应该导入查询和 return 一个可以用参数调用的函数 getAuthorsById
。
这样的东西已经存在了吗?或者是否有任何工具或文档可以帮助我实现所需的功能?
感谢您的帮助!
您可以使用 graphql-tools
模块的 documents-loading 从不同来源加载 GraphQL 操作文档。
例如
index.ts
:
import { GraphQLSchema, buildSchema, graphql } from 'graphql';
import { loadDocumentsSync, GraphQLFileLoader } from 'graphql-tools';
import path from 'path';
const typeDefs: string = `
type Author {
id: ID!
name: String
}
type Query {
authors(ids: [ID]!): [Author]!
}
`;
const resolvers = {
authors({ ids }) {
return [
{ id: ids[0], name: 'a' },
{ id: ids[1], name: 'b' },
];
},
};
const schema: GraphQLSchema = buildSchema(typeDefs);
const query = loadDocumentsSync(path.resolve(__dirname, './query.graphql'), {
loaders: [new GraphQLFileLoader()],
});
graphql({
schema,
source: query[0].rawSDL!,
rootValue: resolvers,
variableValues: { ids: [1337, 42] },
}).then(({ data }) => {
console.log(data);
});
query.graphql
:
query getAuthorsById($ids: [ID]!) {
authors(ids: $ids) {
name
id
}
}
执行结果:
[Object: null prototype] {
authors:
[ [Object: null prototype] { name: 'a', id: '1337' },
[Object: null prototype] { name: 'b', id: '42' } ] }