简体   繁体   English

Relay从哪里获取GraphQL的Schema?

[英]From where does Relay fetch the Schema of GraphQL?

I am having a running GraphQL based server and I am able to test it properly using GraphiQL. 我正在运行基于GraphQL的服务器,我可以使用GraphiQL正确测试它。 But I am unable to understand the implementation of Relay. 但我无法理解Relay的实现。

As GraphQL is on the server side, then how its schema is transferred to relay over network layer at the client end? 由于GraphQL位于服务器端,那么它的架构如何通过客户端的网络层传输到中继? Or how it is pointing to the same GraphQL? 或者它是如何指向相同的GraphQL的?

It takes a little extra work. 这需要一些额外的工作。 Basically you have to dump a serialized version of the schema to a file on the server side, and then move that file over to the client and include it during babel compilation. 基本上,您必须将模式的序列化版本转储到服务器端的文件,然后将该文件移动到客户端并在babel编译期间包含它。 Facebook has a babel plugin that takes this file and builds it into the bundle in order for Relay to know about the schema. Facebook有一个babel插件 ,可以获取此文件并将其构建到捆绑包中,以便Relay了解架构。

EDIT: here is a snippet for how to dump the schema file to JSON 编辑:这是一个如何将模式文件转储到JSON的片段

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) })
      }
    })
  })
}

After you obtain that, you have to npm install babel-relay-plugin and include that in your babel plugins (probably in the webpack config, if you're using that). 获得之后,你必须npm install babel-relay-plugin并将其包含在你的babel插件中(如果你正在使用它,可能在webpack配置中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM