Graphql-config 无法识别 Apollo Graphql @client 指令

Ada*_*m S 5 intellij-idea graphql apollo-client graphql-tag

我将 Apollo Client 与 React、graphql-tag与 Webpack 一起使用,以及graphql-config来维护客户端上的架构。

有一个文件 ./myclient/src/features/stats/graphql/getStart.graphql

query GetStart {
    start @client
}
Run Code Online (Sandbox Code Playgroud)

在哪里start并且@client不使用 IDE graphql 插件进行验证,因为它们不包含在自动生成的模式中。

./myclient/.graphqlconfig文件

{
    "projects": {
    "client": {
      "schemaPath": "schema.graphql",
      "extensions": {
        "endpoints": {
          "dev": "http://localhost:3000/graphql"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Webpack 被配置为在客户端加载 graphql 模式

{
  test: /\.(graphql|gql)$/,
  exclude: /node_modules/,
  use: 'graphql-tag/loader',
},
Run Code Online (Sandbox Code Playgroud)

它将正确加载服务器架构。但是,如何将其配置为验证或忽视start @client,这是造成Unknown field "start" on object "Query"Unknown directive "@client"错误?

Ada*_*m S 6

可以为 Apollo 客户端定义客户端模式,docs。我创建了一个./src/apollo/graphql/typeDefs.graphql包含类型定义的文件。

directive @client on FIELD

type RestParams {
    limit: Int
    page: Int
}

extend type Query {
    restParams: RestParams
}
Run Code Online (Sandbox Code Playgroud)

我将其typeDefs.graphql导入client.js文件并添加typeDefsApolloClient构造函数选项中。

import { ApolloClient } from 'apollo-client';
import { ApolloLink } from 'apollo-link';
import { InMemoryCache } from 'apollo-cache-inmemory';

import TYPE_DEFS from './graphql/typeDefs.graphql';
import createHttpLink from './links/httpLink';
import createErrorLink from './links/errorLink';
import createAuthLink from './links/authLink';

const errorLink = createErrorLink();
const httpLink = createHttpLink();
const authLink = createAuthLink();

const cache = new InMemoryCache({});

const client = new ApolloClient({
  cache,
  link: ApolloLink.from([
    authLink,
    errorLink,
    httpLink,
  ]),
  // resolves,
  typeDefs: TYPE_DEFS,
  connectToDevTools: true,
});

export default client;
Run Code Online (Sandbox Code Playgroud)

IDE 无法发现类型定义,但 Apollo Chrome 检查器插件也可以发现它们。