我正在尝试Relay和GraphQL.当我在做模式时,我这样做:
let articleQLO = new GraphQLObjectType({
name: 'Article',
description: 'An article',
fields: () => ({
_id: globalIdField('Article'),
title: {
type: GraphQLString,
description: 'The title of the article',
resolve: (article) => article.getTitle(),
},
author: {
type: userConnection,
description: 'The author of the article',
resolve: (article) => article.getAuthor(),
},
}),
interfaces: [nodeInterface],
})
Run Code Online (Sandbox Code Playgroud)
所以,当我要求这样的文章时:
{
article(id: 1) {
id,
title,
author
}
}
Run Code Online (Sandbox Code Playgroud)
它会对数据库进行3次查询吗?我的意思是,每个字段都有一个解析方法(getTitle,getAuthor ...),它向数据库发出请求.我做错了吗?
这是getAuthor的一个例子(我使用mongoose):
articleSchema.methods.getAuthor = function(id){
let article = this.model('Article').findOne({_id: id})
return article.author
}
Run Code Online (Sandbox Code Playgroud) 我在创建schema.json文件时遇到问题,该文件可以使用babel-relay-plugin进行解析,而不会遇到错误.看一下relay的example文件夹中包含的schema.json文件,我尝试在GraphiQL中复制查询,但我似乎无法正确使用它.我正在使用Laravel作为后端.这是我可以通过GraphiQL完成的事情,还是向GraphQL端点发送请求并保存响应?
尝试解析schema.json文件时发生错误:
Cannot read property 'reduce' of undefined while parsing file: /Users/username/Sites/Homestead/Code/ineedmg-graphql/resources/assets/js/app.js
Run Code Online (Sandbox Code Playgroud)
最后一次尝试使用GraphiQL:
{
__schema {
queryType {
name
},
types {
kind,
name,
description,
fields {
name,
description,
type {
name,
kind,
ofType {
name
description
}
}
isDeprecated,
deprecationReason,
},
inputFields {
name
description
}
interfaces {
kind
name
description
},
enumValues {
name
description
isDeprecated
deprecationReason
}
},
mutationType {
name
},
directives {
name,
description,
onOperation,
onFragment,
onField,
args {
name
description …Run Code Online (Sandbox Code Playgroud) 我想知道查询类型后面的字符串的重要性(在本例中为“ ProvisionQueues”),从字符串中删除它似乎没有任何影响-仅用于日志记录还是其他。元数据?
mutation ProvisionQueues {
createQueue(name: "new-queue") {
url
}
}
Run Code Online (Sandbox Code Playgroud) 如何为GraphQL中的字符串数组创建对象属性的模式?我希望响应看起来像这样:
{
name: "colors",
keys: ["red", "blue"]
}
Run Code Online (Sandbox Code Playgroud)
这是我的架构
var keysType = new graphql.GraphQLObjectType({
name: 'keys',
fields: function() {
key: { type: graphql.GraphQLString }
}
});
var ColorType = new graphql.GraphQLObjectType({
name: 'colors',
fields: function() {
return {
name: { type: graphql.GraphQLString },
keys: { type: new graphql.GraphQLList(keysType)
};
}
});
Run Code Online (Sandbox Code Playgroud)
当我运行此查询时,我得到一个错误,没有数据,错误就是 [{}]
查询{colors {name,keys}}
但是,当我运行查询只返回名称时,我得到了成功的响应.
查询{colors {name}}
如何在查询密钥时创建一个返回字符串数组的模式?
我正在尝试使用graphql将许多休息端点绑定在一起,而我仍然坚持如何过滤,排序和分页结果数据.具体来说,我需要通过嵌套值进行过滤和/或排序.
在所有情况下,我都无法对其余端点进行过滤,因为它们是具有单独数据库的独立微服务.(即我可以title在其余端点中过滤文章,但不能在author.name上过滤).同样排序.如果没有过滤和排序,也无法在其余端点上进行分页.
为了说明问题,并尝试解决方案,我formatResponse在apollo-server中使用了以下内容,但我想知道是否有更好的方法.
我已经将解决方案归结为我能想到的最小的文件集:
data.js表示2个虚构的休息端点返回的内容:
export const Authors = [{ id: 1, name: 'Sam' }, { id: 2, name: 'Pat' }];
export const Articles = [
{ id: 1, title: 'Aardvarks', author: 1 },
{ id: 2, title: 'Emus', author: 2 },
{ id: 3, title: 'Tapir', author: 1 },
]
Run Code Online (Sandbox Code Playgroud)
模式定义为:
import _ from 'lodash';
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLList,
GraphQLString,
GraphQLInt,
} from 'graphql';
import {
Articles,
Authors,
} from './data';
const …Run Code Online (Sandbox Code Playgroud) 我有一个Article在我的架构中调用的类型:
type Article {
id: ID!
updated: DateTime
headline: String
subline: String
}
Run Code Online (Sandbox Code Playgroud)
对于它的更新,有一个updateArticle(id: ID!, article: ArticleInput!)突变使用的相应输入类型:
input ArticleInput {
headline: String
subline: String
}
Run Code Online (Sandbox Code Playgroud)
突变本身看起来像这样:
mutation updateArticle($id: ID!, $article: ArticleInput!) {
updateArticle(id: $id, article: $article) {
id
updated
headline
subline
}
}
Run Code Online (Sandbox Code Playgroud)
文章始终保存为一个整体(而不是单个字段逐个),所以当我通过了一篇文章,该突变我以前牵强,它会抛出这样的错误Unknown field. In field "updated",Unknown field. In field "__typename"和Unknown field. In field "id".这些有根本原因,那些字段没有在输入类型上定义.
根据规范,这是正确的行为:
(...)此无序映射不应包含任何名称未由此输入对象类型的字段定义的条目,否则应抛出错误.
现在我的问题是处理这些场景的好方法是什么.我应该将应用代码中输入类型允许的属性列入白名单吗?
如果可能的话我想避免这种情况,并且可能有一个实用程序功能将它们切换为我,它知道输入类型.但是,由于客户端不了解架构,因此必须在服务器端进行.因此,不必要的属性将转移到那里,我想这是他们不应该首先转移的原因.
有没有比白名单更好的方法?
我正在使用apollo-client,react-apollo而且graphql-server-express.
为什么GraphQL implements关键字需要复制字段,这是强制性的吗?与文档中的示例一样:
enum Episode { NEWHOPE, EMPIRE, JEDI }
interface Character {
id: String
name: String
friends: [Character]
appearsIn: [Episode]
}
type Human implements Character {
id: String
name: String
friends: [Character]
appearsIn: [Episode]
homePlanet: String
}
type Droid implements Character {
id: String
name: String
friends: [Character]
appearsIn: [Episode]
primaryFunction: String
}
Run Code Online (Sandbox Code Playgroud)
如果是,那么潜在的原因是什么?
因为如果我必须复制,如果我改变那么我需要改变到处...
我将graphql与SQLite数据库连接起来.
在sql中,id是整数,但在graphql id中是字符串.
搜索之后,基于这个问题 - 何时使用GraphQLID而不是GraphQLInt?
说明有三个建议:
它建议第三个.但我不确定在何处以及如何实施它.另外,为什么graphql ID成了String?答案可能会影响ID部分的实现方式.
我正在使用apollo-server和apollo-graphql-tools,我有以下架构
type TotalVehicleResponse {
totalCars: Int
totalTrucks: Int
}
type RootQuery {
getTotalVehicals(color: String): TotalVehicleResponse
}
schema {
query: RootQuery
}
Run Code Online (Sandbox Code Playgroud)
和解析器功能是这样的
{
RootQuery: {
getTotalVehicals: async (root, args, context) => {
// args = {color: 'something'}
return {};
},
TotalVehicleResponse: {
totalCars: async (root, args, conext) => {
// args is empty({}) here
.........
.........
},
totalTrucks: async (root, args, conext) => {
// args is empty({}) here
.........
.........
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在任何子args解析器中访问root解析器(getTotalVehicals)中可用的?
我正在尝试timeout设置prisma-labs/graphql-request. 我已经尝试过此处描述的方法 - https://github.com/prisma-labs/graphql-request/issues/103。
const client = new GraphQLClient(config.url, {
timeout: 30000,
headers: {
Authorization: `Bearer ${token}`
}
})
Run Code Online (Sandbox Code Playgroud)
我的编译器抱怨超时没有直接出现在选项界面中 - https://github.com/prisma-labs/graphql-request/blob/master/src/types.ts#L7。
我是否需要扩展选项接口以使用超时字段?
graphql ×10
graphql-js ×10
javascript ×3
apollo ×2
reactjs ×2
relayjs ×2
node.js ×1
react-apollo ×1
sql ×1
sqlite ×1