5

我正在使用带有 React 的 Apollo 客户端、使用Webpack 加载的graphql-taggraphql-config来维护客户端上的模式。

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

query GetStart {
    start @client
}

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

./myclient/.graphqlconfig档案_

{
    "projects": {
    "client": {
      "schemaPath": "schema.graphql",
      "extensions": {
        "endpoints": {
          "dev": "http://localhost:3000/graphql"
        }
      }
    }
  }
}

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

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

它将正确加载服务器模式。但是,如何配置它以验证或忽略start @client导致错误的原因Unknown field "start" on object "Query"Unknown directive "@client"错误?

4

1 回答 1

6

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

directive @client on FIELD

type RestParams {
    limit: Int
    page: Int
}

extend type Query {
    restParams: RestParams
}

我将其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;

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

于 2019-07-09T02:37:02.920 回答