以下是我使用GraphQL架构字符串创建架构并将其附加到Express服务器的方法:
var graphql = require('graphql');
var graphqlHTTP = require('express-graphql');
[...]
return graphqlHTTP({
schema: graphql.buildSchema(schemaText),
rootValue: resolvers,
graphiql: true,
});
Run Code Online (Sandbox Code Playgroud)
这是模块的所有非常基本的用法.它运行良好,非常方便,直到我想定义一个联合:
union MediaContents = Photo|Youtube
type Media {
Id: String
Type: String
Contents: MediaContents
}
Run Code Online (Sandbox Code Playgroud)
我发现无法使这项工作,查询内容做它必须做的事情,返回正确的对象,但失败的消息Generated Schema cannot use Interface or Union types for execution.
使用buildSchema时是否可以使用联合?
我收到一个奇怪的错误,无法弄清楚我做错了什么。我写了一个graphql突变来调用一个api:
domainStuff: async (parent, { command, params }, { models }) => {
console.log("Params:", params);
const result = await dd24Api(command, params);
return result;
}
Run Code Online (Sandbox Code Playgroud)
这是我调用的函数:
export default async (command, parameter) => {
const args = _.merge({ params: parameter }, auth);
// Eleminate copying mistakes
console.log(typeof args);
const properCommand = command + "Async";
const result = await soap
.createClientAsync(apiWSDL)
.then(client => {
return client[properCommand](args)
.then(res => {
console.log(res[command + "Result"]);
return res[command + "Result"];
})
.catch(err => {
console.log(err);
return err; …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试解析一个简单的配方列表,其中包含对食材的引用。
数据布局如下所示:
type Ingredient {
name: String!
amount: Int!
unit: Unit!
recipe: Recipe
}
type Recipe {
id: Int!
name: String!
ingredients: [Ingredient]!
steps: [String]!
pictureUrl: String!
}
Run Code Online (Sandbox Code Playgroud)
据我了解,我的解析器应如下所示:第一个解析食谱,第二个解析食谱中的成分字段。根据我的理解,它可以使用配方提供的参数。在我的配方对象中,该成分由id(int)引用,因此这应该是参数(至少我是这样认为的)。
var root = {
recipe: (argument) => {
return recipeList;
},
Recipe: {
ingredients: (obj, args, context) => {
//resolve ingredients
}
},
Run Code Online (Sandbox Code Playgroud)
这些解析器通过以下方式传递到应用程序:
app.use('/graphql', graphqlHTTP({
schema: schema,
graphiql: true,
rootValue: root,
}));
Run Code Online (Sandbox Code Playgroud)
但是,我的解析器似乎没有被调用。我希望在查询中即时查询所有成分。
该端点有效,但是一旦我查询成分,"message": "Cannot return null for non-nullable field Ingredient.name.",就会返回此消息的错误。
当尝试在解析器中记录传入的参数时,我可以看到它从未执行过。不幸的是,我找不到像我一样使用express-graphql时如何使用它的示例。
如何在express-graphQL中为嵌套类型编写单独的解析器?
让我们使用返回一项的基本 mongodb 查询:
const result = await db.myCollection.findById('xxxx')
return result;
Run Code Online (Sandbox Code Playgroud)
给 graphql 的这个查询结果工作正常。
但是现在,如果我返回 a result.toObject(),它将不再起作用。
我收到以下错误:
"message": "Cannot return null for non-nullable field MyCollection.id."
Run Code Online (Sandbox Code Playgroud)
为什么用toObject()之间的映射_id和id不能做?
我有一个由 JWT 授权的 graphql 端点。我的 JWT 策略验证 JWT,然后将用户对象添加到请求对象。
在宁静的路线中,我会像这样访问我的用户数据:
router.get('/', (req, res, next) => {
console.log('user', req.user)
}
Run Code Online (Sandbox Code Playgroud)
我想在我的 graphql 解析器中访问 req.user 对象以提取用户的 ID。但是,当我尝试记录context变量时,它始终为空。
我是否需要配置我的 graphql 端点以将req数据传递给解析器?
我的 app.js 有我的 graphql 设置如下:
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
app.use('/graphql', [passport.authenticate('jwt', { session: false }), bodyParser.json()], graphqlExpress({ schema }));
Run Code Online (Sandbox Code Playgroud)
然后我有我的解析器:
const resolvers = {
Query: {
user: async (obj, {email}, context) => {
console.log('obj', obj) // undefined
console.log('email', email) // currently passed through in graphql query …Run Code Online (Sandbox Code Playgroud) 我正在为我的 GraphQL API 苦苦挣扎,因为我需要利用 Enums,同时在前端保留它们的完整描述。
简而言之:我对产品有不同的状态:"Has been sent", "Not sent yet", "Received"。所以这是使用枚举的正确位置:
enum ProductState {
HAS_BEEN_SENT
NOT_SENT_YET
RECEIVED
}
Run Code Online (Sandbox Code Playgroud)
但是我需要在前端显示正确的字符串(“ Has been sent”,而不是“ HAS_BEEN_SENT”)。
我不能使用简单的解决方案作为“用空格替换下划线并将字符串小写”,因为我的 API 不是英语而是法语(所以我有重音和特殊字符)。
枚举不能返回字符串吗?还是对象? 我尝试使用指令但无法让它工作......
实际上,我不在乎它是如何写入数据库(大写或小写形式)或 GraphQL API 中的。我只需要我的客户以“法语”形式访问不同的产品状态。
我正在尝试向gatsby-starter-hero-blog添加新模板,但我对新模板的 GraphQL 查询被拒绝:
warning The GraphQL query in the non-page component
"/Users/mc/workspaces/mc/src/templates/NoteBookTemplate.js" will not be run.
Queries are only executed for Page or Layout components. Instead of a query,
co-locate a GraphQL fragment and compose that fragment into the query (or other
fragment) of the top-level page or layout that renders this component.
For more
info on fragments and composition see:
http://graphql.org/learn/queries/#fragments
Run Code Online (Sandbox Code Playgroud)
文件夹结构如下:
--src
--components
--images
--pages
--templates
--CategoryTemplate.js
--NotebookTemplate.js
--PageTemplate.js
--PostTemplate.js
--theme
--utils
Run Code Online (Sandbox Code Playgroud)
NotebookTemplate.js 是我添加的新模板(用于使用 Nteract 的 …
在我的数据库中,该Event字段是用字符串数组动态填充的,而有些是用文本手动填充的(无数组)。当我执行我的查询时,只填充一个字符串(不在数组中)的结果显示为空。
架构中的字段是: Event: [String]
GraphQL 有没有办法同时分配 [String] 和 String 类型的字段?我尝试了以下操作,但出现语法错误:
union test = [String] | String
Event: test
Run Code Online (Sandbox Code Playgroud)
注意:手动分配的不在数组中。
尝试使用 react 中继,今天遇到了这个问题。这是我到目前为止所做的。
根查询:
query {
tasks {
id
taskName
taskStatus
userId
}
}
Run Code Online (Sandbox Code Playgroud)
反应组件层次结构
App
?--TaskList (props: tasks)
?--TaskListItem (props: task)
Run Code Online (Sandbox Code Playgroud)
现在由于托管原则,我知道我必须在每个组件中编写片段来描述它们的数据需求。
任务列表项.js
const TaskListItemContainer = createFragmentContainer(
TaskListItem,
graphql`
fragment TaskListItem_task on task {
id
taskName
taskDone
authorId
}
`
);
Run Code Online (Sandbox Code Playgroud)
任务列表.js
const TaskListContainer = createFragmentContainer(
TaskList,
graphql`
fragment TaskList_tasks on task {
tasks {
...TaskListItem_task
}
}
`
);
Run Code Online (Sandbox Code Playgroud)
应用程序.js
<QueryRenderer
environment={relayEnvironment}
query={graphql`
query AppQuery {
...TaskList_tasks
}
`
}
Run Code Online (Sandbox Code Playgroud)
当我运行中继编译器时,出现以下错误。
Fragment "TaskList_tasks" cannot be spread here as …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Postman 通过 Graphql 调用服务。从我的应用程序 (React) 中,我可以毫无问题地使用所有服务(因此,它们运行正常),但我想用 Postman 单独测试它们。
我正在使用 AppSync,这是我的配置:
const AppSyncConfig = {
url: environments.configAws.graphql.aws_appsync_graphqlEndpoint,
region: environments.configAws.graphql.aws_appsync_region,
auth: {
type: AUTH_TYPE.API_KEY,
apiKey: environments.configAws.graphql.aws_appsync_apiKey,
}
};
Run Code Online (Sandbox Code Playgroud)
环境变量是这样的:
graphql: {
aws_project_region: "us-east-1",
aws_appsync_graphqlEndpoint: "https://XXXXXXXXXXXXX.appsync-api.us-east-1.amazonaws.com/graphql",
aws_appsync_region: "us-east-1",
aws_appsync_authenticationType: "API_KEY",
aws_appsync_apiKey: "da2-XXXXXXXXXXXX"
}
Run Code Online (Sandbox Code Playgroud)
该应用程序使用它来进行查询:
const client = new AWSAppSyncClient(AppSyncConfig, {
link: createAppSyncLink({ ...AppSyncConfig,
resultsFetcherLink: ApolloLink.from([
setContext((request, previousContext) => ({
headers: { ...previousContext.headers,
Authorization: localStorage.getItem('token') ? localStorage.getItem('token') : ''
}
})),
createHttpLink({
uri: AppSyncConfig.url
})
])
})
});
const data_configs = client.readQuery({ query: …Run Code Online (Sandbox Code Playgroud) graphql-js ×10
graphql ×8
node.js ×3
aws-appsync ×1
express ×1
gatsby ×1
graphql-java ×1
javascript ×1
jwt ×1
mongodb ×1
mongoose ×1
node-soap ×1
react-apollo ×1
react-relay ×1
reactjs ×1
relay ×1
relayjs ×1
relaymodern ×1