2017-10-15 47 views

回答

0

這裏是一個解決辦法(劈?)我想到了,但可能有更好的方法來做到這一點:

  1. 獲取遠程模式通過使用introspectSchemamakeRemoteExecutableSchema
  2. 獲取模式類型DEFS通過使用printSchema
  3. 將printSchema收到的根類型定義QueryMutation重命名爲其他東西,例如GitHubQueryGitHubMutation
  4. 創建具有與類型github字段根查詢的typedef GitHubQuery
  5. 創建使用execute方法運行在遠程GitHub的架構

源代碼GitHubQuery一個github解析器: https://launchpad.graphql.com/3xlrn31pv

import 'apollo-link' 
import fetch from 'node-fetch' 
import { 
    introspectSchema, 
    makeExecutableSchema, 
    makeRemoteExecutableSchema, 
} from 'graphql-tools' 
import { HttpLink } from 'apollo-link-http' 
import { execute, printSchema } from 'graphql' 

const link = new HttpLink({ uri: 'http://api.githunt.com/graphql', fetch }) 

async function getGithubRemoteSchema() { 
    return makeRemoteExecutableSchema({ 
    schema: await introspectSchema(link), 
    link, 
    }) 
} 

async function makeSchema() { 
    const githubSchema = await getGithubRemoteSchema() 
    const githubTypeDefs = printSchema(githubSchema) 

    const typeDefs = ` 
     ${githubTypeDefs // ugly hack #1 
     .replace('type Query', 'type GitHubQuery') 
     .replace('type Mutation', 'type GitHubMutation')} 

    type Query { 
     github: GitHubQuery 
    } 

    type Mutation { 
     github: GitHubMutation 
    } 
    ` 

    return makeExecutableSchema({ 
    typeDefs, 
    resolvers: { 
     Query: { 
     async github(parent, args, context, info) { 
      // TODO: FIX THIS 

      // ugly hack #2 
      // remove github root field from query 
      const operation = { 
      ...info.operation, 
      selectionSet: 
       info.operation.selectionSet.selections[0].selectionSet, 
      } 
      const doc = { kind: 'Document', definitions: [operation] } 

      const result = await execute(
      githubSchema, 
      doc, 
      info.rootValue, 
      context, 
      info.variableValues, 
      info.operation.name 
     ) 

      return (result || {}).data 
     }, 
     }, 
    }, 
    }) 
} 

export const schema = makeSchema() 
相關問題