GraphQL 渐进学习 04-graphql-resolver

2018-05-13  本文已影响18人  会煮咖啡的猫咪

GraphQL 渐进学习 04-graphql-resolvers-interfaces-接口的使用

目标

代码

步骤

1. 准备测试静态数据

const notices = [{id: 1, content: '这是 notice', noticeTime: 1524710641}]
const reminds = [{id: 1, content: '这是 remind', endTime: 1524710641}]

2. 编写 typeDefs

const typeDefs = `
  scalar Date

  interface Message {
    content: String
  }

  type Notice implements Message {
    content: String
    noticeTime: Date
  }

  type Remind implements Message {
    content: String
    endTime: Date
  }

  type Query {
    searchInterface (text: String!): Message!
  }
`

3. 编写 resolvers

const resolvers = {
  Query: {
    searchInterface: (_, {text}) => {
      if (text === 'notice') {
        return notices[0]
      } else {
        return reminds[0]
      }
    }
  },

  Message: {
    __resolveType(obj, context, info){
      console.log(obj, context, info)
      if(obj.noticeTime){
        return 'Notice'
      }
      if(obj.endTime){
        return 'Remind'
      }
      return null
    }
  },

  Date: new GraphQLScalarType({
    name: 'Date',
    description: 'Date custom scalar type',
    parseValue(value) {
      return new Date(value) // value from the client
    },
    serialize(value) {
      // return new Date(value).getTime()
      return new Date(value) // value sent to the client
    },
    parseLiteral(ast) {
      if (ast.kind === Kind.INT) {
        return parseInt(ast.value, 10) // ast value is always in string format
      }
      return null
    }
  })
}

4. 合并 Schema

const schema = makeExecutableSchema({
  typeDefs,
  resolvers
})

测试

1. 请求 searchInterface 方法

# 请求 query
query do($text: String!) {
  searchInterface(text: $text) {
    content
    ... on Notice {
      noticeTime
    }
    ... on Remind {
      endTime
    }
  }
}

# 变量 variables
{
  "text": "notice"
}

# 输出
{
  "data": {
    "searchInterface": {
      "content": "这是 notice",
      "endTime": "1970-01-18T15:31:50.641Z"
    }
  }
}
# 请求 query
query do($text: String!) {
  searchInterface(text: $text) {
    ... on Notice {
      content
      noticeTime
    }
    ... on Remind {
      content
      endTime
    }
  }
}

# 变量 variables
{
  "text": "notice"
}

# 输出
{
  "data": {
    "searchInterface": {
      "content": "这是 remind",
      "endTime": "1970-01-18T15:31:50.641Z"
    }
  }
}

详细请移步 05-graphql-resolvers-union-联合的使用

2. 打印 __resolveType(obj, context, info) 参数

# obj
{ id: 1, content: '这是 notice', noticeTime: 1524710641 } 

# context
{}

# info
{ fieldName: 'searchInterface',
  fieldNodes:
   [ { kind: 'Field',
       alias: undefined,
       name: [Object],
       arguments: [Array],
       directives: [],
       selectionSet: [Object],
       loc: [Object] } ],
  returnType: Message,
  parentType: Query,
  path: { prev: undefined, key: 'searchInterface' },
  schema:
   GraphQLSchema {
     __allowedLegacyNames: undefined,
     _queryType: Query,
     _mutationType: Mutation,
     _subscriptionType: null,
     _directives: [ [Object], [Object], [Object] ],
     astNode: undefined,
     _typeMap:
      { Query: Query,
        Post: Post,
        Int: Int,
        String: String,
        Author: Author,
        Country: Country,
        Message: Message,
        MessageResult: MessageResult,
        Notice: Notice,
        Date: Date,
        Remind: Remind,
        Mutation: Mutation,
        AuthorInput: AuthorInput,
        __Schema: __Schema,
        __Type: __Type,
        __TypeKind: __TypeKind,
        Boolean: Boolean,
        __Field: __Field,
        __InputValue: __InputValue,
        __EnumValue: __EnumValue,
        __Directive: __Directive,
        __DirectiveLocation: __DirectiveLocation },
     _implementations: { Message: [Array] },
     __validationErrors: [],
     _possibleTypeMap: { Message: [Object] } },
  fragments: {},
  rootValue: undefined,
  operation:
   { kind: 'OperationDefinition',
     operation: 'query',
     name: { kind: 'Name', value: 'do', loc: [Object] },
     variableDefinitions: [ [Object] ],
     directives: [],
     selectionSet: { kind: 'SelectionSet', selections: [Array], loc: [Object] },
     loc: { start: 0, end: 160 } },
  variableValues: { text: 'notice' } }

参考

上一篇下一篇

猜你喜欢

热点阅读