这是我用于获取用户详细信息的示例 graphql 查询(我在 ReactJS 中使用 Apollo 客户端)
如果我只需要id和name作为响应,我使用此查询
{
getUserDetails {
id
name
}
}
Run Code Online (Sandbox Code Playgroud)
或者如果我只需要id和userType我使用此查询:
{
getUserDetails {
id
userType
}
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以动态传递字段名称和用户类型(基本上动态传递字段)而不是再次编写整个查询?
我收到来自外部 API 的 LASTUPDATE: 1579443364 响应,
如何在架构中定义数据类型以便将其转换为日期格式?
像这样的格式: Mon Jan 19 2020 00:44:04
GraphQL 模式语言支持:
Int
Float
字符串
Id
布尔值
我正在尝试使用 GraphQL(Apollo 服务器)查询单个 MongoDB 文档 ( trivia),但其中一个文档字段遇到问题。
LightningRoundQuestion.answer并且PictureRoundPicture.answer应该返回一个String,并且MultipleChoiceRoundQuestion.answer应该返回一个Int。查看架构:
schema
const typeDefs = gql`
# ROOT TYPES ==================================================
type Query {
trivia(_id: String!): Trivia
}
# INTERFACES ==================================================
interface Round {
type: String!
theme: String!
pointValue: Int!
}
type LightningRound implements Round {
type: String!
theme: String!
pointValue: Int!
questions: [LightningRoundQuestion]
}
type MultipleChoiceRound implements Round {
type: String!
theme: String!
pointValue: Int!
questions: [MultipleChoiceRoundQuestion]
}
type PictureRound implements Round { …Run Code Online (Sandbox Code Playgroud) 我一直在努力弄清楚如何让文件上传在 graphql 中工作。
\n这是我的基本实现。
\n\n// eslint-disable-next-line import/no-extraneous-dependencies\nconst { ApolloServer, gql } = require(\'apollo-server-express\')\n// eslint-disable-next-line import/no-extraneous-dependencies\nconst express = require(\'express\')\n\n\nconst typeDefs = gql` \n type File {\n filename: String!\n mimetype: String!\n encoding: String!\n }\n \n type Query {\n _ : Boolean\n }\n \n type Mutation {\n singleUpload(file: Upload!): File!,\n singleUploadStream(file: Upload!): File!\n }\n`;\n\nconst resolvers = {\n Mutation: {\n singleUpload: (parent, args) => {\n return args.file.then(file => {\n const {createReadStream, filename, mimetype} = file\n\n const fileStream = createReadStream()\n\n return file;\n });\n …Run Code Online (Sandbox Code Playgroud) graphql-js buildSchema和apollo-server 有什么区别gql?他们似乎做着非常相似的工作。
我有一个超级简单的 GraphQl 查询,但我无法根据输入使查询动态化。假设您将在 javascript 中获得一个字符串,并将其传递给查询,它是如何完成的?以下面的示例为例,我如何替换产品中 Sku 字段上的硬编码字符串“3111”,而是插入变量 myString 中的值?当我尝试传递它时,我只是收到错误。
let myString = "3111"
`query getProductBySku {
site {
product(sku: "3111") {
id
entityId
name
sku
}
}
}`
Run Code Online (Sandbox Code Playgroud) 下面我试图example用一个object-arg 设置一个突变credentials。我以前曾进行过此工作,然后突然停止在JSON部分上工作失败。为什么我无法通过发送json credentials?
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLInputObjectType,
GraphQLNonNull,
graphql
} from 'graphql'
let requestType = new GraphQLInputObjectType({
name: 'Request',
fields: {
name: {type: GraphQLString},
}
})
let responseType = new GraphQLObjectType({
name: 'Response',
fields: {
name: {type: GraphQLString},
age: {type: GraphQLInt}
}
})
let schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
author: {
type: GraphQLString,
resolve: (source, args, context, info) => {
return 'Thomas Reggi'
}
}
} …Run Code Online (Sandbox Code Playgroud) 我在Google上搜索过类似内容,cypress request with graphql但我看到很多人都在谈论mock up server,stub等等。但是我无法找到完整的示例以及如何cypress.io与graphqlusing请求一起使用。
有谁知道它是如何cy.request工作的,graphql或者在哪里可以找到它的解决方案?
在此先感谢您的帮助。
我正在为GraphQL模式编写一个变体:
const Schema = new GraphQLSchema({
mutation: new GraphQLObjectType({
name: 'Mutation',
fields: () => ({
person: {
type: GraphQLString,
args: {
name: {type: GraphQLString},
school: {type: GraphQLString},
},
resolve: mutatePerson,
},
}),
}),
});
Run Code Online (Sandbox Code Playgroud)
我想确保mutatePerson只有同时存在name和school参数时,该方法才能起作用。我该如何检查?
我有一个突变:
const createSomethingMutation = gql`
mutation($data: SomethingCreateInput!) {
createSomething(data: $data) {
something {
id
name
}
}
}
`;
Run Code Online (Sandbox Code Playgroud)
如何Something在一个请求中创建多个?我需要像这样在我的GraphQL服务器上创建一个新的Mutation吗:
mutation {
addManySomethings(data: [SomethingCreateInput]): [Something]
}
Run Code Online (Sandbox Code Playgroud)
还是有一种方法可以createSomethingMutation在一个请求中多次使用来自Apollo Client的现有方法和不同的参数?
graphql-js ×10
graphql ×9
javascript ×4
apollo ×2
node.js ×2
react-apollo ×2
reactjs ×2
cypress ×1
graphql-tag ×1
json ×1
unit-testing ×1