我在查询 graphql/relay 中的某些字段时遇到问题。我正在尝试在下面的架构中查询 Courses 上的“courseName”字段。
如果我查询用户,例如:
{ 用户(id:1){名字,姓氏}}
它工作并返回正确的响应。
这是我的架构:
import {
GraphQLBoolean,
GraphQLFloat,
GraphQLID,
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
} from 'graphql';
import {
connectionArgs,
connectionDefinitions,
connectionFromArray,
fromGlobalId,
globalIdField,
mutationWithClientMutationId,
nodeDefinitions,
} from 'graphql-relay';
import {
// Import methods that your schema can use to interact with your database
User,
Course,
getUser,
getCourses
} from './database';
/**
* We get the node interface and field from the Relay library.
*
* The first method defines the way we …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用GraphQL和React Relay构建应用程序.
作为其中的一部分,我使用以下规范创建了一个根查询:
query {
AllServices {
edges {
node {
id
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以我创建了一个名为CreateGithubService的突变 - 效果很好.
以下是Relay变异的片段:
getFatQuery() {
return Relay.QL`
fragment on CreateGithubServicePayload {
createdService
}
`
}
getConfigs() {
return [{
type: "REQUIRED_CHILDREN",
children: [
Relay.QL`
fragment on CreateGithubServicePayload {
createdService {
id
}
}
`
]
}]
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是这不会导致依赖于信息更新的视图.
我的AllServices查询未被重新获取,我无法在Fat Query中指定它.
如何设置我的变异以将元素添加到所有服务查询?
谢谢!
我的猫鼬模式如下
var ImageFormats = new Schema({
svg : String,
png-xlarge : String,
png-small : String
});
Run Code Online (Sandbox Code Playgroud)
当我将其翻译成 GraphQL 模式时,这就是我尝试的
export var GQImageFormatsType: ObjectType = new ObjectType({
name: 'ImageFormats',
fields: {
svg : { type: GraphQLString },
'png-xlarge': { type: GraphQLString },
'png-small' : { type: GraphQLString }
}
});
Run Code Online (Sandbox Code Playgroud)
GraphQL 返回以下错误: Error: Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "png-xlarge" does not.
如果我尝试在 Mongoose 模型之后对 GraphQL 进行建模,我该如何协调这些字段?有没有办法让我创建别名?
(我在 graffiti 和 stackoverflow 论坛上搜索过这个,但找不到类似的问题)
我试图找出如何使用graphql突变变异嵌套对象,如果可能的话.例如,我有以下架构:
type Event {
id: String
name: String
description: String
place: Place
}
type Place {
id: String
name: String
location: Location
}
type Location {
city: String
country: String
zip: String
}
type Query {
events: [Event]
}
type Mutation {
updateEvent(id: String, name: String, description: String): Event
}
schema {
query: Query
mutation: Mutation
}
Run Code Online (Sandbox Code Playgroud)
如何在我的updateEvent变异中添加地点信息?
我已将我与该文件的连接分离出来并可以在我的根查询中使用它(当在类型中定义连接时它工作正常),但是当尝试通过将其导入到类型时使用它,连接是为了我回来:
错误:PageEdge.node字段类型必须是输出类型,但得到:undefined
我可以在QueryType中使用连接,但不能使用PageType.我的架构大致是这样的.
QueryType {
pages: [PageType]
}
PageType {
_id: string
pages: [PageType]
}
Run Code Online (Sandbox Code Playgroud)
这是一个项目仓库的链接,其中graphql的东西是(我只是推了一个提交,错误地看到所有的代码):https: //github.com/DaveyEdwards/myiworlds/tree/master/src/data
我已经使QueryType和PageType字段thunk(似乎解决了大多数人的问题的解决方案),并尝试从接口中创建一个thunk:
interfaces: () => [nodeInterface]
Run Code Online (Sandbox Code Playgroud)
对于任何与用户和朋友构建应用程序的人来说,我认为这是一个非常常见的问题.
UserType: {
friends: [UserType]
}
Run Code Online (Sandbox Code Playgroud)
我的PageType:
import { nodeInterface } from '../nodeInterface';
import PageConnection from './connections/PageConnection';
const PageType = new ObjectType({
name: 'Page',
description: 'Everything you see can be placed inside a page.',
fields: () => ({
id: globalIdField('Page', page => page._id),
_id: {
type: new NonNull(ID),
description: 'A unique id used to instantly locate …Run Code Online (Sandbox Code Playgroud) 我将如何在具有可选参数的 nodeJS 中实现 GraphQL Mutation?
我当前的突变有一个 args 字段,但所有参数都是强制性的。由于我在文档中找不到任何内容,因此我不知道这是否可行或是否可行。
这是我当前的代码:
const fakeDB = {
count: 0
};
const schema = new GraphQLSchema({
query: //...
mutation: new GraphQLObjectType({
name: 'adsF',
fields: {
updateCount: {
type: GraphQLInt,
args: {
count: { type: GraphQLInt } // I want to make this argument optional
},
resolve: (value, { count }) => {
// Catch if count is null or undefined
if (count == null) {
// If so just update with default
fakeDB.count = 5; …Run Code Online (Sandbox Code Playgroud) 我刚刚掌握了GraphQL,
我已经设置了以下查询:
type: UserType,
args: {
id: { name: 'id', type: new GraphQLNonNull(GraphQLID) },
email: { name: 'email', type: new GraphQLNonNull(GraphQLString) }
},
resolve: (root, { id, email }, { db: { User } }, fieldASTs) => {
...
}
Run Code Online (Sandbox Code Playgroud)
我希望能够将'id'或'email'传递给查询,但是,通过此设置,它需要传递id和电子邮件.
有没有办法设置查询所以只需要一个参数,无论是id还是电子邮件,但不是两者都有?
对于我的MongoDB对象,我有一个架构,其中有许多嵌套数据。
但是我在GraphQL中完美实现了查询以获取数据,但是由于无法定义类型,因此无法获取嵌套数据。
fields() {
return {
name: {
type: GraphQLString,
description: 'Name of the person'
},
age: {
type: GraphQLString,
description: 'Age'
},
documents: { // what to do here
type: new GraphQLList(),
description: 'All documents for the person'
}
}
}
Run Code Online (Sandbox Code Playgroud)
原始数据是这样的。
{
"_id" : ObjectId("5ae1da5e4f00b5eee4ab84ee"),
"name" : "Indira Gandhi International Airport",
"age" : 54,
"documents" : [
{
"doc_name" : "personal card",
"doc_url" : "http://",
"status" : true
},
{
"doc_name" : "bank card",
"doc_url" : "http://",
"status" …Run Code Online (Sandbox Code Playgroud) 在express-graphql应用程序中,我有一个userLogin解析器,如下所示:
const userLogin = async ({ id, password }), context, info) => {
if (!id) {
throw new Error('No id provided.')
}
if (!password) {
throw new Error('No password provided.')
}
// actual resolver logic here
// …
}
Run Code Online (Sandbox Code Playgroud)
如果用户不提供idAND AND password,则只会抛出一个错误。
{
"errors": [
{
"message": "No id provided.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"userLogin"
]
}
],
"data": {
"userLogin": null
}
}
Run Code Online (Sandbox Code Playgroud)
如何在errors响应数组中引发多个错误?
现在我在下面有这个标签。它是静态的,将始终获得ID为3的注释。是否有可能在此graphQL-tag中放入变量。因此,我可以重新使用graphQL-tag,仅更改变量ID?
export const GET_COMMENTS: any = gql`
{
comments(id: 3) {
userId,
text,
creationDate,
}
}
`;
Run Code Online (Sandbox Code Playgroud)
提前致谢!
graphql-js ×10
graphql ×9
javascript ×4
relayjs ×3
mongoose ×2
node.js ×2
graphql-tag ×1
mongodb ×1
reactjs ×1
relay ×1