这是中继官方文档中的代码,这是用于 GraphQLAddTodoMutation
const GraphQLAddTodoMutation = mutationWithClientMutationId({
name: 'AddTodo',
inputFields: {
text: { type: new GraphQLNonNull(GraphQLString) },
},
outputFields: {
todoEdge: {
type: GraphQLTodoEdge,
resolve: ({localTodoId}) => {
const todo = getTodo(localTodoId);
return {
cursor: cursorForObjectInConnection(getTodos(), todo),
node: todo,
};
},
},
viewer: {
type: GraphQLUser,
resolve: () => getViewer(),
},
},
mutateAndGetPayload: ({text}) => {
const localTodoId = addTodo(text);
return {localTodoId};
},
});
Run Code Online (Sandbox Code Playgroud)
我认为 mutateAndGetPayload 先执行,然后再执行 outputFields?因为它使用 localTodoId 对象作为参数,所以我看到了从 mutateAndGetPayload 返回的 localTodoId 对象。
这是中继突变的代码。请查看 getFatQuery
export default class AddTodoMutation extends …Run Code Online (Sandbox Code Playgroud) 使用facebook的参考库,我发现了一种破解通用类型的方法,如下所示:
type PagedResource<Query, Item> = (pagedQuery: PagedQuery<Query>) => PagedResponse<Item>
?
interface PagedQuery<Query> {
query: Query;
take: number;
skip: number;
}
?
interface PagedResponse<Item> {
items: Array<Item>;
total: number;
}
Run Code Online (Sandbox Code Playgroud)
function pagedResource({type, resolve, args}) {
return {
type: pagedType(type),
args: Object.assign(args, {
page: { type: new GraphQLNonNull(pageQueryType()) }
}),
resolve
};
function pageQueryType() {
return new GraphQLInputObjectType({
name: 'PageQuery',
fields: {
skip: { type: new GraphQLNonNull(GraphQLInt) },
take: { type: new GraphQLNonNull(GraphQLInt) }
}
});
}
function pagedType(type) {
return new …Run Code Online (Sandbox Code Playgroud) 是否可以同时利用 GraphQL 和 Mongoose?
到目前为止,我已经能够集成 GraphQL 和 Mongoose 来处理数据库填充,但我很难理解它如何检索数据,特别是具有嵌套引用的数据。
考虑这个模式:
const fooSchema = new Schema({
name: { type: 'String', required: true },
bar: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Bar',
required: false,
}],
});
Run Code Online (Sandbox Code Playgroud)
Bar 模式本质上是相同的,只有一个“name”字段。
是否可以运行 GraphQL 查询来使用“bar”中的引用填充数据?
目前,我们正在使用 GraphQL-Tools 来创建 typeDefs、Mutations 和 Queries,如下所示:
const typeDefs = `
type Foo {
name: String!,
bars:[Bar]
}
type Bar {
_id: ID,
name: String,
}
type Query {
allFoos: [Foo!]!
foo(_id: ID!): Foo
}
type Mutation {
...
}
`;
module.exports = makeExecutableSchema({typeDefs, …Run Code Online (Sandbox Code Playgroud) 更新:apollo已更新代码和文档,因此问题现在无关紧要
预期结果
使用https://github.com/apollographql/apollo-tutorial-kit中的以下代码段启动apollo-server-express .
import express from 'express';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import bodyParser from 'body-parser';
import schema from './data/schema';
const GRAPHQL_PORT = 3000;
const graphQLServer = express();
graphQLServer.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
graphQLServer.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));
graphQLServer.listen(GRAPHQL_PORT, () => console.log(`GraphiQL is now running on http://localhost:${GRAPHQL_PORT}/graphiql`));
Run Code Online (Sandbox Code Playgroud)
实际结果:
在apollo-server-express的任何实现中,使用docs,使用https://github.com/apollographql/apollo-tutorial-kit等.我一遍又一遍地收到以下堆栈跟踪:
Error: Must provide document
at invariant (~/node_modules/graphql/jsutils/invariant.js:18:11)
at Object.validate (~/node_modules/graphql/validation/validate.js:58:34)
at doRunQuery (~/node_modules/apollo-server-core/dist/runQuery.js:80:38)
at ~/node_modules/apollo-server-core/dist/runQuery.js:20:54
at <anonymous>
Run Code Online (Sandbox Code Playgroud)
如何重现问题:
git clone https://github.com/apollographql/apollo-tutorial-kit.git
cd apollo-tutorial-kit
yarn/npm …Run Code Online (Sandbox Code Playgroud) 因此,我尝试在 MongoDB 中创建一个 User 集合,并使用 GraphQL 和 mongoose 对其进行查询。
我已经在路径“pathToServer\server\models\user.js”中创建了用户架构,它看起来像这样:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
name: {
firstName: String,
lastName: String,
},
email: String,
password: String,
})
module.exports = mongoose.model('User', userSchema);
Run Code Online (Sandbox Code Playgroud)
我创建了一个 GraphQL 类型,目前我将它放在路径“pathToServer\server\schema\types\user.js”中,它看起来像这样:
const graphql = require('graphql');
const {GraphQLObjectType, GraphQLList, GraphQLInt, GraphQLID, GraphQLString, GraphQLSchema, GraphQLNonNull} = graphql;
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
email: {type: GraphQLString},
name: new GraphQLObjectType({
firstName: {type: GraphQLString},
lastName: …Run Code Online (Sandbox Code Playgroud) 我构建了一个 graphQL 服务器来包装多个 Restful API。我将集成的一些 API 是第三方的,而一些是我们拥有的。我们使用 redis 作为缓存层。如果我在 graphQL 上实现数据加载器缓存可以吗?它会影响我现有的 redis 缓存吗?
我看过一些教程和示例,它们有时使用parent有时使用root作为他们在 graphql 解析器中的第一个参数。
在什么情况下正确的命名是什么?为什么?
例如(nodejs):
signup: (root, args, context, info) => signup(root, args, context, info)
Run Code Online (Sandbox Code Playgroud)
对比
signup: (parent, args, context, info) => signup(parent, args, context, info)
Run Code Online (Sandbox Code Playgroud)
或在进行注册的函数中:
const signup(root, args, context, info) = {
// do magic stuff
}
Run Code Online (Sandbox Code Playgroud)
对比
const signup(parent, args, context, info) = {
// do magic stuff
}
Run Code Online (Sandbox Code Playgroud) 从 GraphQL / Node / Express / Typescript 堆栈开始,我遇到了一个问题,字段参数没有传递给解析器函数。我有两个疑问:
用户列表:
UserList: {
type: new GraphQLList(UserType),
args: {
userName: {
type: GraphQLString
},
userIsLocked: {
type: GraphQLBoolean
},
userIsConfirmed: {
type: GraphQLBoolean
},
userAwaitingPassword: {
type: GraphQLBoolean
}
},
resolve: ((_, {fields}) => new User().where({fields}))
}
Run Code Online (Sandbox Code Playgroud)
和用户:
User: {
type: UserType,
args: {
userId: {
type: GraphQLID
}
},
resolve: ((_, {userId}) => new User().getById({userId}))
}
Run Code Online (Sandbox Code Playgroud)
如果未提供条件,该User().where()函数默认显示所有用户。但是,请考虑以下查询和结果:
query All {
UserList(userIsLocked:true){
userId
userName
userIsLocked
}
}
// Results
{ …Run Code Online (Sandbox Code Playgroud) 我有一个 Apollo GraphQL 服务器与 API 通信,返回响应的大致结构如下:
{
"pagination": {
"page": 1,
// more stuff
},
sorting: {
// even more stuff
},
data: [ // Actual data ]
}
Run Code Online (Sandbox Code Playgroud)
这个结构将在我广泛使用的这个 API 的几乎所有响应中共享。data大多数情况下将是一个数组,但也可以是一个对象。
我如何以有效的方式编写它,以便我不必在模式中的每个数据类型上重复所有这些pagination和sorting字段?
非常感谢!
我在这里定义了一个graphQL对象:
const graphql = require('graphql');
const { GraphQLObjectType } = require('graphql');
const ProductType = new GraphQLObjectType({
name: 'Product',
description: 'Product GraphQL Object Schemal Model',
fields: {
productId: { type: graphql.GraphQLString },
SKU: { type: graphql.GraphQLString },
quantity: { type: graphql.GraphQLFloat },
unitaryPrice: { type: graphql.GraphQLFloat },
subTotal: { type: graphql.GraphQLFloat },
discount: { type: graphql.GraphQLFloat },
totalBeforeTax: { type: graphql.GraphQLFloat },
isTaxAplicable: { type: graphql.GraphQLBoolean },
unitaryTax: { type: graphql.GraphQLFloat },
totalTax: { type: graphql.GraphQLFloat }
}
});
module.exports.ProductType = …Run Code Online (Sandbox Code Playgroud) 我正在运行两个单独的docker服务。一个用于我的GraphQL服务器,另一个用于棱镜服务,该服务连接到本地Postgres数据库。我能够运行Arizona部署并直接在中进行测试http://localhost:4466。但是,当我尝试使用中的应用程序的GraphQL服务器进行查询时http://localhost:8080,它将给出以下响应。
{
"data": null,
"errors": [
{
"message": "request to http://localhost:4466/ failed, reason: connect ECONNREFUSED 127.0.0.1:4466",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"feed"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪。
graphql-server_1 | [Network error]: FetchError: request to http://localhost:4466/ failed, reason: connect ECONNREFUSED 127.0.0.1:4466
graphql-server_1 | Error: request to http://localhost:4466/ failed, reason: connect ECONNREFUSED 127.0.0.1:4466
graphql-server_1 | at new CombinedError (/usr/src/app/node_modules/graphql-binding/node_modules/graphql-tools/dist/stitching/errors.js:83:28)
graphql-server_1 | at Object.checkResultAndHandleErrors (/usr/src/app/node_modules/graphql-binding/node_modules/graphql-tools/dist/stitching/errors.js:101:15)
graphql-server_1 | at CheckResultAndHandleErrors.transformResult (/usr/src/app/node_modules/graphql-binding/node_modules/graphql-tools/dist/transforms/CheckResultAndHandleErrors.js:10:25)
graphql-server_1 | at …Run Code Online (Sandbox Code Playgroud) 我知道我的数据是否会像
{
id: 123
address: xyz
name: hello
}
Run Code Online (Sandbox Code Playgroud)
我想在查询期间使用别名我可以这样做
query {
identification: id
address
full_name : name
}
Run Code Online (Sandbox Code Playgroud)
所以我的数据将如下所示:
{
identification: 123
address: xyz
full_name: hello
}
Run Code Online (Sandbox Code Playgroud)
但我希望即将发布的数据如下所示:
{
id : 123
info: {
address: xyz
name: hello
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 GraphQl Query 中实现这一点,我怎样才能给出别名info
graphql ×12
graphql-js ×12
node.js ×5
apollo ×3
javascript ×2
mongoose ×2
relay ×2
mongodb ×1
prisma ×1
react-apollo ×1
reactjs ×1
relayjs ×1