【发布时间】:2017-09-07 17:38:21
【问题描述】:
我有一个正在运行的基于 GraphQL 的服务器,我能够使用 GraphiQL 对其进行正确测试。但我无法理解 Relay 的实现。
既然 GraphQL 是在服务端,那么它的 schema 是如何在客户端通过网络层传输到中继的呢?或者它是如何指向同一个 GraphQL 的?
【问题讨论】:
标签: reactjs graphql relayjs relay graphql-js
我有一个正在运行的基于 GraphQL 的服务器,我能够使用 GraphiQL 对其进行正确测试。但我无法理解 Relay 的实现。
既然 GraphQL 是在服务端,那么它的 schema 是如何在客户端通过网络层传输到中继的呢?或者它是如何指向同一个 GraphQL 的?
【问题讨论】:
标签: reactjs graphql relayjs relay graphql-js
这需要一些额外的工作。基本上,您必须将模式的序列化版本转储到服务器端的文件中,然后将该文件移动到客户端并在 babel 编译期间包含它。 Facebook 有一个 babel plugin 接收这个文件并将其构建到包中,以便 Relay 了解架构。
编辑:这是一个关于如何将模式文件转储到 JSON 的 sn-p
import { graphql } from 'graphql'
import { introspectionQuery, printSchema } from 'graphql/utilities'
/*
generates json of our schema for use by relay
*/
export default function (schema) {
return new Promise((resolve, reject) => {
graphql(schema, introspectionQuery).then(result => {
if (result.errors) {
console.error(`ERROR introspecting schema: ${result.errors}`)
reject(new Error(result.errors))
} else {
resolve({ json: result, graphql: printSchema(schema) })
}
})
})
}
在你获得它之后,你必须 npm install babel-relay-plugin 并将它包含在你的 babel 插件中(可能在 webpack 配置中,如果你正在使用它的话)。
【讨论】: