GraphQL and Schema-First Development

GraphQL and Schema-First Development

GraphQL NightでのLT発表資料です

1604058c93731a1f795ce62d37d19fd5?s=128

Kōhei Yamamoto

June 28, 2018
Tweet

Transcript

  1. ,ōIFJ:BNBNPUP!LZNNU (SBQI2-φΠτ-5 (SBQI2-ͱ εΩʔϚϑΝʔετ։ൃ

  2. (.0ϖύϘΧϥʔϛʔϦϐʔτCMPHLZNNUDPN ,ōhei Yamamoto /!LZNNU SVCZSBJMTZPLPIBNBSC 

  3. None
  4. ͋ΒͨΊͯ঺հ͠·͢

  5. εΩʔϚϑΝʔετ։ൃ wϑϩϯτΤϯυ '& ͱόοΫΤϯυ #& ؒͷ
 ΠϯλʔϑΣʔεఆٛΛ։ൃνʔϜ಺ͰઌʹܾΊΔ wͦͷ͋ͱ'&#&ʹ෼͔Εͯฒߦ։ൃ͢Δ

  6. ฒߦ։ൃ w'&͸#&ͷ࣮૷͕ऴΘΔ·ͰϞοΫαʔόΛ࢖ͬͨΓ ͢Δ w3&45ͷ৔߹ w0QFO"1* 4XBHHFS ͰઌʹΠϯλϑΣʔεΛॻ͘ wपลπʔϧ 4XBHHFS$PEFHFOͳͲ Λ࢖ͬͯ


    ϞοΫαʔόΛ࡞Δ
  7. ΠϯλʔϑΣʔεఆٛྫ 0QFO"1*ͷ৔߹ paths: /pets/{petId}: get: parameters: - name: petId in:

    path required: true schema: type: string responses: '200': content: application/json: schema: required: - id - name properties: id: type: integer format: int64 name: type: string (&5QFUTQFU@JE
  8. ΧϥʔϛʔϦϐʔτ͸ εΩʔϚϑΝʔετ։ൃ͍ͯ͠·͢ '&7VFKT #&3BJMT

  9. (SBQI2-ͰεΩʔϚϑΝʔετ։ൃ w3&45"1*ͰεΩʔϚΛܾΊ͔ͯΒ'&ͱ#&Ͱ
 ฒߦ։ൃΛਐΊΔͷ͸ศརͩͬͨ wҰ෦(SBQI2-Λಋೖͯ͠Έ͍ͯΔ w3BJMTͳͷͰHSBQIRMSVCZ w(SBQI2-Ͱ΋ಉ͡Α͏ʹͰ͖ͳ͍͔ʁ

  10. (SBQI2-ʹ͓͚ΔεΩʔϚϑΝʔετͱ͸ wεΩʔϚ͚ͩઌʹܾΊΔ w#&͸SFTPMWFSΛ͋ͱͰ࣮૷͢Δ w'&͕࢖͏ϞοΫαʔό͸Ͳ͏͢Δʁ

  11. "QPMMPͱHSBQIRMSVCZΛ
 ૊Έ߹ΘͤͯϞοΫαʔόΛ࡞ΕΔ

  12. HSBQIRMSVCZ w3VCZͷγϯλοΫεͰ(SBQI2-εΩʔϚΛॻ͘ w(SBQI2-ͷεΩʔϚΛμϯϓͰ͖Δ

  13. 3VCZͰεΩʔϚΛॻ͘ class Types::QueryType < Types::BaseObject field :viewer, Types::UserType, null: true

    # resolverΛॻ͍ͯͳ͍ʂ end class Types::UserType < Types::BaseObject field :email, String, null: true field :popularPosts, [Types::PostType], null: true # resolverΛॻ͍ͯͳ͍ʂ end class Types::PostType < Types::BaseObject field :title, String, null: true end
  14. HSBQIRMSVCZͰεΩʔϚΛμϯϓ FooBarSchema.to_definition # ҎԼͷจࣈྻΛऔಘ͢Δ type Query { viewer: User }

    type User { email: String popularPosts: [Post!] } type Post { title: String }
  15. "QPMMP w"QPMMPͷ(SBQI2-5PPMTΛ࢖͏ w.PDLJOHͱ͍͏ػೳ͕͋Δ w(SBQI2-ͷεΩʔϚΛ৯ΘͤΔͱϞοΫαʔό͕
 ࡞ΕΔ

  16. (SBQI2-5PPMTͰϞοΫαʔό // express, graphql-express, body-parserͳͲΛrequire import { addMockFunctionsToSchema, makeExecutableSchema }

    = require('graphql-tools'); const schema = makeExecutableSchema({ /* μϯϓͨ͠εΩʔϚจࣈྻ */ }); const mocks = require('./mocks') addMockFunctionsToSchema({ schema, mocks }); const app = express(); app.use('/graphql', bodyParser.json(), graphqlExpress({ schema })); app.listen(3000, () => { console.log('GraphQL mock server is running!!1'); });
  17. (SBQI2-5PPMTͰϞοΫαʔό // mocks.js module.exports = { User: () => ({

    email: 'kymmt90@example.com', }), Post: () => ({ title: 'The Awesome Post', }), }; ϞοΫαʔό͕ฦ͢μϛʔσʔλΛॻ͚Δ ࢦఆ͠ͳ͚Ε͹εΧϥʔܕͷ஋͸"QPMMP͕ ద౰ʹฦ͢
  18. ϞοΫαʔόͷ1045HSBQIRMΛୟ͍༷ͨࢠ

  19. "QPMMPͱHSBQIRMSVCZΛ
 ૊Έ߹ΘͤͯϞοΫαʔόΛ࡞Εͨ

  20. εΩʔϚϑΝʔετ։ൃͰ͖Δʂ
 ศརʂʂ

  21. ৄ͘͠͸ͪ͜ΒͰ HJUIVCDPNLZNNUTDIFNBpSTUHSBQIRMFYBNQMF

  22. ࢀߟ wIUUQCMPHLZNNUDPNFOUSZNPDLJOHHSBQIRMTFSWFSXJUI SBJMTBOEBQPMMP wIUUQTXXXBQPMMPHSBQIRMDPNEPDTHSBQIRMUPPMT NPDLJOHIUNM