Graphql 阿波罗在当地的客户不';t触发本地解析器

Graphql 阿波罗在当地的客户不';t触发本地解析器,graphql,apollo,Graphql,Apollo,对于本地状态客户端(前端本地状态),Apollo不会触发解析器。阿波罗2.7 有人知道为什么会这样吗 以下是设置: 阿波罗客户机 import { ApolloClient } from 'apollo-client' import { InMemoryCache } from 'apollo-cache-inmemory' import { HttpLink } from 'apollo-link-http' import fetch from 'isomorphic-unfetch' im

对于本地状态客户端(前端本地状态),Apollo不会触发解析器。阿波罗2.7

有人知道为什么会这样吗

以下是设置:

阿波罗客户机

import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { HttpLink } from 'apollo-link-http'
import fetch from 'isomorphic-unfetch'

import { resolvers, typeDefs } from './resolvers';
import { initCache } from './init-cache';

export default function createApolloClient(initialState, ctx) {
  // The `ctx` (NextPageContext) will only be present on the server.
  // use it to extract auth headers (ctx.req) or similar.
  return new ApolloClient({
    ssrMode: Boolean(ctx),
    link: new HttpLink({
      uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute)
      credentials: 'include', // Additional fetch() options like `credentials` or `headers`
      fetch,
    }),
    typeDefs,
    resolvers,
    connectToDevTools: true,
    cache: initCache({
                   robot: {
                     __typename: 'Robot',
                     name: 'Robbie',
                     status: 'live',
                    },

                  member: {
                     __typename: 'Member',
                     name: 'RFesagfd',
                     }
                   }),
     })
  }
import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { HttpLink } from 'apollo-link-http'
import fetch from 'isomorphic-unfetch'

import { resolvers, typeDefs } from './resolvers';
import { initCache } from './init-cache';

export default function createApolloClient(initialState, ctx) {
  // The `ctx` (NextPageContext) will only be present on the server.
  // use it to extract auth headers (ctx.req) or similar.
  return new ApolloClient({
    ssrMode: Boolean(ctx),
    link: new HttpLink({
      uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute)
      credentials: 'include', // Additional fetch() options like `credentials` or `headers`
      fetch,
    }),
    typeDefs,
    resolvers,
    connectToDevTools: true,
    cache: initCache({
                     author: {
                     __typename: 'Author',
                     posts: 0,
                     name: '' // NEED TO SET AN INITIAL VALUE
                     }
     })
  }
类型和解析器(resolvers.js)

质疑


谢谢你的帮助

您需要定义本地查询的结果类型:

const typeDefs = gql`
  extend type Query {
    robot: Robot
    member: Member
  }
。。。和查询的解析器-而不是键入(当您将整个查询修饰为本地时)。。。但是您必须返回键入的数据

export const resolvers = {
  Query: {
    member: (...args) => {
      console.log('args', args); 
      return {
        __typename: 'Member',
        name: 'some name', // read from cache
        isLogged: true // function result
      };
    }
  }
};
您还应该使用
\uuuu typename
进行缓存写入

更新 假设缓存中有一个Memeber。。。你可以:

// read (initialized with permanent) data:
const memberData = cache.readQuery(....
// f.e. it should have `__typename` and 'name`
// ... and 'decorate' it with derived properites
memberData.age = currentYear - memberData.birthYear;
memberData.isLogged = someFuncReturningBool();
return memberData; // Member type shaped object
它是关于形状/数据组织-类型化(返回具有定义属性的类型化对象)或简单(单独返回所有属性)或混合,例如(某些全局应用程序状态)


我找到了一个可能的解决办法。也许这些信息对某些人有用。 如果我们想省略查询解析器+字段解析器,并且我们想拥有唯一的字段解析器,我们需要使用@client(always:true)

深入的解释

通常,Apollo客户端如何使用缓存存在问题。 默认情况下,它缓存响应,下次将从缓存中获取缓存结果(例如)。即使在客户机的情况下,这种行为也是相同的。

这意味着当缓存中有初始模型时,Apollo将从缓存中获取并忽略解析器,即使我们传递@client指令。 为了解决这个问题并让Apollo知道,即使我们有一个缓存对象,我们也需要使用本地解析器,我们需要对首选字段或整个对象使用@client(总是:true)。我在下面举了一个例子。

另外,不幸的是,我没有发现如何强制Apollo处理不存在的字段,因此如果我们想为特定字段使用解析程序,我们仍然需要在初始缓存模型中定义初始字段值,以便让Apollo知道该字段。之后,Apollo将使用解析器为该字段生成一些高计算输出,这要归功于@client(总是:true)
一般来说,这是可以的,因为我们应该知道我们的模型中会有什么样的动态场

阿波罗客户机

import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { HttpLink } from 'apollo-link-http'
import fetch from 'isomorphic-unfetch'

import { resolvers, typeDefs } from './resolvers';
import { initCache } from './init-cache';

export default function createApolloClient(initialState, ctx) {
  // The `ctx` (NextPageContext) will only be present on the server.
  // use it to extract auth headers (ctx.req) or similar.
  return new ApolloClient({
    ssrMode: Boolean(ctx),
    link: new HttpLink({
      uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute)
      credentials: 'include', // Additional fetch() options like `credentials` or `headers`
      fetch,
    }),
    typeDefs,
    resolvers,
    connectToDevTools: true,
    cache: initCache({
                   robot: {
                     __typename: 'Robot',
                     name: 'Robbie',
                     status: 'live',
                    },

                  member: {
                     __typename: 'Member',
                     name: 'RFesagfd',
                     }
                   }),
     })
  }
import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { HttpLink } from 'apollo-link-http'
import fetch from 'isomorphic-unfetch'

import { resolvers, typeDefs } from './resolvers';
import { initCache } from './init-cache';

export default function createApolloClient(initialState, ctx) {
  // The `ctx` (NextPageContext) will only be present on the server.
  // use it to extract auth headers (ctx.req) or similar.
  return new ApolloClient({
    ssrMode: Boolean(ctx),
    link: new HttpLink({
      uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute)
      credentials: 'include', // Additional fetch() options like `credentials` or `headers`
      fetch,
    }),
    typeDefs,
    resolvers,
    connectToDevTools: true,
    cache: initCache({
                     author: {
                     __typename: 'Author',
                     posts: 0,
                     name: '' // NEED TO SET AN INITIAL VALUE
                     }
     })
  }
类型和解析器(resolvers.js)

质疑


export const resolvers={成员:{
-
成员
小写?…您需要整个成员的解析程序HI xadm。感谢您的帮助。成员是一种类型,因此它不应该像查询解析程序那样采用小写形式。请您向我展示完整成员解析程序的示例好吗?我想我做得很正确。如果我想要一个预定义的初始成员,该怎么办del在缓存中,我希望有一些计算字段,这些字段不应该显示在缓存中,但应该由解析程序显示,这些解析程序可以从成员缓存中生成一些值并生成这些新字段?例如,fullName。如何实现它?如果缓存中已经有成员查询解析程序,那么我应该有一个成员查询解析程序吗?我想要的只是一些ad传统的“计算”字段?谢谢xadm。这是一个很酷的解决方案,看起来可能会起作用。但是,还有没有一种方法可以在不执行查询->成员解析程序的情况下为这些字段创建解析程序?我希望这些内容保持干净和分离。例如:对初始缓存中不存在的对象使用查询解析程序(动态生成)和具有初始缓存中现有对象的不存在字段的解析程序。“默认情况下,@基于客户端的字段以与远程字段完全相同的方式利用缓存。运行本地解析程序后,其结果将与任何远程结果一起缓存。”-必须键入缓存数据,不存在“不存在字段”的情况(缓存写入时忽略其他字段-就像在远程服务器上一样,解析器可以返回更多字段,但响应会过滤为类型/请求形状)…但是您可以使用customJSON类型(读取文档)在某些对象中存储(和读取)任何内容。
import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { HttpLink } from 'apollo-link-http'
import fetch from 'isomorphic-unfetch'

import { resolvers, typeDefs } from './resolvers';
import { initCache } from './init-cache';

export default function createApolloClient(initialState, ctx) {
  // The `ctx` (NextPageContext) will only be present on the server.
  // use it to extract auth headers (ctx.req) or similar.
  return new ApolloClient({
    ssrMode: Boolean(ctx),
    link: new HttpLink({
      uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute)
      credentials: 'include', // Additional fetch() options like `credentials` or `headers`
      fetch,
    }),
    typeDefs,
    resolvers,
    connectToDevTools: true,
    cache: initCache({
                     author: {
                     __typename: 'Author',
                     posts: 0,
                     name: '' // NEED TO SET AN INITIAL VALUE
                     }
     })
  }
import gql from 'graphql-tag';
import { print  } from 'graphql';

export const typeDefs = gql`
  type Author {
    posts: Int!
    name: String
  }

`;


export const resolvers = {
  Author: {
    name(author) {
      console.log('Author name resolver', author). // WORKS
      return 'NAME';
    },
  },

};
const GET_AUTHOR = gql`
  query getAuthor {
    author {
      posts
      name @client(always: true)
    }
  }
`;