2017-02-01 21 views
0

私はちょうどreact/graphqlを使ってコードを作成する方法を学び始めました。パラメータの受け渡しの仕組みを理解するのは非常に困難です。下のコード例では、https://github.com/graphql/swapi-graphqlから取得した場合、resolve関数が引数 "edge"と "conn"をいつ入力するのかわかりません。誰かが私にいくつかの洞察を与えることができますか?Graphqlパラメータの受け渡し

export function connectionFromUrls(
    name: string, 
    prop: string, 
    type: GraphQLOutputType 
): GraphQLFieldConfig<*, *> { 
    const {connectionType} = connectionDefinitions({ 
    name, 
    nodeType: type, 
    resolveNode: edge => getObjectFromUrl(edge.node), 
    connectionFields:() => ({ 
     totalCount: { 
     type: GraphQLInt, 
     resolve: conn => conn.totalCount, 
     description: 
`A count of the total number of objects in this connection, ignoring pagination. 
This allows a client to fetch the first five objects by passing "5" as the 
argument to "first", then fetch the total count so it could display "5 of 83", 
for example.` 
     }, 
     [prop]: { 
     type: new GraphQLList(type), 
     resolve: conn => conn.edges.map(edge => getObjectFromUrl(edge.node)), 
     description: 
`A list of all of the objects returned in the connection. This is a convenience 
field provided for quickly exploring the API; rather than querying for 
"{ edges { node } }" when no edge data is needed, this field can be be used 
instead. Note that when clients like Relay need to fetch the "cursor" field on 
the edge to enable efficient pagination, this shortcut cannot be used, and the 
full "{ edges { node } }" version should be used instead.` 
     } 
    }) 
    }); 
    return { 
    type: connectionType, 
    args: connectionArgs, 
    resolve: (obj, args) => { 
     const array = obj[prop] || []; 
     return { 
     ...connectionFromArray(array, args), 
     totalCount: array.length 
     }; 
    }, 
    }; 
} 

答えて

1

GraphQLエグゼキュータは、クエリを処理するときにresolve関数を呼び出します。開発者は、エグゼキュータがどのように動作するかを管理する必要はありません。あなたの唯一の目標は、フィールドが返すものを(resolve関数で)指定することです。エグゼキュータについて知る必要があるのは、クエリ・ツリーのすべてのブランチに到達するまで、再帰的に各フィールドを呼び出し、解決されたオブジェクトを階層の下に渡すことだけです。

あなたはGraphQLタイプを定義するときは、(例えばタイプUserフィールドがタイプAddressなどのものとすることができるaddress呼ばれています)どのタイプの各フィールドに戻り、彼らが持っているフィールド内容を指定し、なりますresolve機能クエリに応答して実行される。 resolve関数の最初のパラメータは常に親オブジェクトです。この場合、conn。 (実際にはedgeresolveNodeに渡されますが、これは接続エッジを処理するカスタムメソッドですが、この質問の範囲を超えています)

関連する問題