Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GraphQL Directiveを試してみた

piglovesyou
December 21, 2018

GraphQL Directiveを試してみた

GraphQL Directiveに可能性を感じたので、Apollo Serverでカスタムディレクティブを作ってみたのと、その考察です。

piglovesyou

December 21, 2018
Tweet

More Decks by piglovesyou

Other Decks in Programming

Transcript

  1. // .js import { SchemaDirectiveVisitor } from 'apollo-server'; class ConcatDirective

    extends SchemaDirectiveVisitor { visitFieldDefinition(fieldDefinition) { const { urls } = this.args; fieldDefinition.resolve = (obj, args, context, info) => Promise.all( urls.map( url => fetch(url).then( res => res.json() ) ) ).then( rs => rs.reduce((xs, arr) => [...xs, ...arr], []), ); } } export const schemaDirectives = { concat: ConcatDirective, };
  2. # .graphql type Issue { id: String url: String }

    type RootQuery { # ... ghissues: [Issue] @concat(urls: [ "https://api.github.com/repos/apollographql/apollo-link/issues", "https://api.github.com/repos/apollographql/apollo-client/issues", "https://api.github.com/repos/apollographql/apollo-server/issues", ]) }
  3. # the graphql response { data: { ghissues: [ {

    id: 393044392, url: https://api.github.com/repos/apollographql/apollo-link/issues/897 }, // ... { id: 392713767, url: https://api.github.com/repos/apollographql/apollo-client/issues/4252 }, // ... { id: 386114424, url: https://api.github.com/repos/apollographql/apollo-server/issues/2051 } ] } }
  4. // ./dev-graphql-server.js import { ApolloServer } from 'apollo-server'; global.__DEV__ =

    true; const { default: schema } = require('./src/data/schema'); new ApolloServer(schema).listen().then(({ url }) => { console.log(` Server ready at ${url}`); }); (SBQI2-αʔόͷΈΛ5SZ&SSPSͰ͖ΔΑ͏ʹ͓ͯ͘͠
  5. w (SBQI2-ͷޙΖͷ࣮૷Λແͤ͘Δເ w ϑϩϯτΤϯυΤϯδχΞ͕ɺ(SBQI2-εΩʔϚͱϑϩϯτͷ
 ίʔυΛॻ͚ͩ͘Ͱׂͱڽͬͨ8FCΞϓϦΛσϓϩΠ͢Δະདྷ w ྫ͑͹!NZTRM΍!SFEJTͳͲͱ͍ͬͨσΟϨΫςΟϒΛ࡞Ε͹ɺ
 ετϨʔδ઀ଓ෦෼ͷ࣮૷ͳ͠Ͱσʔλऔಘɾॻ͖׵͑Ͱ͖ͦ͏ɻ
 !BVUI΋࢓༷ΛݻΊΒΕΕ͹͔ͳΓεέʔϧͦ͠͏ w

    ΧελϜσΟϨΫςΟϒҰࣜΛ഑෍ͯ͠Δ੎
 IUUQTXXXOQNKTDPNQBDLBHFHSBQIRMDVTUPNEJSFDUJWFT
 !EBUF BTz:::: ͱ͔͸͸·Ε͹ڧͦ͏ʣ w (SBQI2-εΩʔϚͰ%#εΩʔϚ΋ࡁ·ͤΔ੎
 IUUQTXXXOQNKTDPNQBDLBHFHSBQIRMEJSFDUJWFTRM