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

Introduction to using GraphQL for a bit

Kazuki Ohashi
December 19, 2018

Introduction to using GraphQL for a bit

Kazuki Ohashi

December 19, 2018
Tweet

More Decks by Kazuki Ohashi

Other Decks in Technology

Transcript

  1. 4DIFNBͱ͸ʁ w ͲͷσʔλΛ΍ΓͱΓ͢Δ͔ ͷ಺༰ΛܾΊΔ #BDLFOEଆ  w ϑϩϯτΤϯυͱόοΫΤϯ υͷܖ໿ type

    Query { hero: Hero } type Hero { name: String friends: [Friend] } type Friend { name: String age: Int address: String } ༧໿ޠʂSPVUFͷఆٛΈ͍ͨͳ΋Μ ಠࣗͷܕΛఆٛͰ͖Δ ෳ਺ฦ͢৔߹͕͋Δ৔߹͸഑ྻʹ͢Δ
  2. 2VFSZͱ͸ʁ w (FUϝιουͷ࣌ར༻ w ཉ͍͠σʔλ͚ͩهड़͢Ε͹ ͦΕ͕ฦͬͯ͘Δ { hero { name

    friends { name } } } ཉ͍͠ΦϒδΣΫτͷ ϑΟʔϧυ໊Λॻ͘ { hero(id: 1) { name friends { name } } } ˞εΩʔϚ͸มߋ͢Δඞཁ͋Γ ৚݅Λࢦఆ͢Δ͜ͱ΋Ͱ͖Δ
  3. .VUBUJPOͱ͸ʁ extend type Mutation { createClientAccount( client_id: Int @rules(apply: ["required"])

    staff_name: String @rules(apply: ["required"]) tel: String @rules(apply: ["required"]) department: String @rules(apply: ["required"]) email: String @rules(apply: ["required"]) password: String @rules(apply: ["required"]) role: Int @rules(apply: ["required"]) ): ClientAccount @create(model: "App\\Models\\ClientAccount") } w 1045165%&-&5&ϝιουͷ࣌ར༻ w ༧໿ޠ ϝιου ܕͱόϦσʔγϣϯ
 Λࢦఆ -BSBWFM
  4. *OQVUͱ͸ʁ extend type Mutation { createClientAccount2(input: CreateClientAccount): ClientAccount
 @create(model: "App\\Models\\ClientAccount",

    flatten: true) } input CreateClientAccount { client_id: Int @rules(apply: ["required"]) staff_name: String @rules(apply: ["required"]) tel: String @rules(apply: ["required"]) department: String @rules(apply: ["required"]) email: String @rules(apply: ["required"]) password: String @rules(apply: ["required"]) role: Int @rules(apply: ["required"]) } w .VUBUJPOͳͲͰར༻͢Δೖྗ஋Λ0CKFDUʹ·ͱΊΔ
  5. &EHFͱ/PEF type InfluencerSearchPage implements Page { paginatorInfo: PaginatorInfo edges: [InfluencerSearchEdge]

    } type InfluencerSearchEdge implements Edge { cursor: ID! node: InfluencerSearchNode } w ϖʔδωʔγϣϯ࣌ʹ&EHFͱ/PEFͷ֓೦Λར༻ w &EHF͸/PEFΛ΋ͪɺ/PEF͸$VSTPSΛ࣋ͭ
  6. εΩʔϚͷఆٛ extend type Query @group(middleware: ["auth:api"]) { client_account(id: ID @eq):

    ClientAccount @find(model: "App\\Models\\ClientAccount") client_accounts: [ClientAccount!]! @all } type ClientAccount { id: ID! client_id: Int! staff_name: String! tel: String! department: String! email: String! role: Int! created_at: DateTime! updated_at: DateTime! } ϛυϧ΢ΣΞ͕࢖͑Δ Ϟσϧͦͷ··࢖͏ͳΒ͜͜ͰఆٛͰ͖Δ
  7. ಠࣗͷఆٛ extend type Query @group(middleware: ["auth:api"]) { fetchProjects: [PrProject!]! }

    type PrProject { id: ID! item_id: Int! project_type: Int! name: String! media_type: Int! item_detail: String! item_image: String! hash_tags: String } .PEFMTʹఆ͕ٛͳ͍΋ͷ
  8. 6TF$BTF͔Βͷฦ٫ <?php namespace App\Http\GraphQL\Queries; use App\Domains\UseCases\FetchProjectList; use GraphQL\Type\Definition\ResolveInfo; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;

    class FetchProjects extends BaseQuery { public function resolve($rootValue, array $args, GraphQLContext $context = null, ResolveInfo $resolveInfo) { $usecase = app(FetchProjectList::class); return $usecase($this->client_id); } } Ϩεϙϯεͷத਎͸ܕʹม׵͞ΕΔ
  9. ΫΤϦʔͷఆٛ const GET_SEARCH_INFLUENCER = gql` query ($page: Int!, $name: String){

    query_search_influencers(page: $page, influencer_name: $name) { paginatorInfo { total count currentPage } edges { node { id influencer_name instagram_posts { id code comment_count is_video date } } } } } `;
  10. ΫΤϦʔͷൃߦ render() { return ( <div className="GraphList3" style={{ width: "500px"

    }}> <Query query={GET_SEARCH_INFLUENCER} variables={{ page: 1, name: "ా" }} > {({ data, error, loading, client, refetch }) => { if (error) return <p>{error.toString()}</p>; let pageInfo, graphData, dataElms; if (!loading) { pageInfo = data.query_search_influencers.paginatorInfo; graphData = data.query_search_influencers.edges; dataElms = graphData && graphData.map((v) => { let data = v.node; return ( <div key={data.id}>{data.influencer_name}</div> ); }); } return ( <div className="GraphList" style={{ padding: "40px"}}> . . . 2VFSZλά಺ͰΫΤϦʔΛൃߦͰ͖Δ Ϩεϙϯεσʔλ͕དྷΔͷͰ
 ͍ͭ΋௨Γͷॲཧ