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

FlutterでGraphQL のuseQueryを使う

Avatar for iganin iganin
November 14, 2024

FlutterでGraphQL のuseQueryを使う

Flutter Tokyo #5 登壇資料

Avatar for iganin

iganin

November 14, 2024
Tweet

More Decks by iganin

Other Decks in Technology

Transcript

  1. React.js GraphQL const GET_SAMPLE = gql` query GetSample($id: ID!) {

    sample(id: $id) { id name } } `; function SampleView({ id }: { id: string }) { const { loading, error, data } = useQuery(GET_SAMPLE, { variables: { id }, }); if (loading) return <p>ಡΈࠐΈத...</p>; if (error) return <p>Τϥʔ͕ൃੜ͠·ͨ͠: {error.message}</p>; return <p>{data?.sample?.name}</p>; } export default SampleView;
  2. React.js GraphQL const GET_SAMPLE = gql` query GetSample($id: ID!) {

    sample(id: $id) { id name } } `; function SampleView({ id }: { id: string }) { const { loading, error, data } = useQuery(GET_SAMPLE, { variables: { id }, }); if (loading) return <p>ಡΈࠐΈத...</p>; if (error) return <p>Τϥʔ͕ൃੜ͠·ͨ͠: {error.message}</p>; return <p>{data?.sample?.name}</p>; } export default SampleView;
  3. graphql_codegen x hooks • graphql_ fl utter READMEͷgraphql_codegenʹؔ͢Δهࡌ • This

    package generate hooks and options which takes away the struggle of serialization and gives you con fi dence through type- safety. • graphql_codegen READMEͷهࡌ • fl utter_hooks (optional) to use generated operations hooks. Will be inside HookWidgets
  4. Query query Sample($id: ID!) { sample(id: $id) { __typename id

    name } } sample.graphqlͷϑΝΠϧ໊Ͱ࡞੒
  5. ࢖ͬͯΈΔ class SamplePage extends HookWidget { const SamplePage({super.key}); @override Widget

    build(BuildContext context) { final query = useQuery$Sample( Options$Query$Sample( fetchPolicy: FetchPolicy.cacheAndNetwork, variables: Variables$Query$Sample(id: '1'), ), ); if (query.result.isLoading) { return const Scaffold(body: CircularProgressIndicator()); } if (query.result.hasException) { return const Scaffold(body: Text('error')); } return Scaffold(body: Text(query.result.parsedData?.sample.name ?? 'Sample')); } }
  6. ࢖ͬͯΈΔ class SamplePage extends HookWidget { … … @override Widget

    build(BuildContext context) { final query = useQuery$Sample( Options$Query$Sample( fetchPolicy: FetchPolicy.cacheAndNetwork, variables: Variables$Query$Sample(id: '1'), ), ); … … } } HookWidgetͰ࢖༻ buildϝιου಺Ͱ useQueryΛ࢖༻͢Δ FetchPolicy΋࢖͑Δ
  7. ࢖ͬͯΈΔ class SamplePage extends HookWidget { … @override Widget build(BuildContext

    context) { final query = useQuery$Sample( ……… ); if (query.result.isLoading) { return const Scaffold( body: CircularProgressIndicator()); } if (query.result.hasException) { return const Scaffold(body: Text('error')); } return Scaffold( body: Text(query.result.parsedData?.sample.name ?? 'Sample')); } } query͔Βloadingத Τϥʔ͔Ͳ͏͔ औಘ݁Ռ ͕औΕ·͢
  8. function SampleView() { const { loading, error, data } =

    useQuery(GET_SAMPLE, { variables: { “1” }, }); if (loading) return <p>ಡΈࠐΈத...</p>; if (error) return <p>Τϥʔ</p>; return <p>{data?.sample?.name}</p>; } export default SampleView; class SamplePage extends HookWidget { const SamplePage({super.key}); @override Widget build(BuildContext context) { final query = useQuery$Sample( Options$Query$Sample( variables: Variables$Query$Sample(id: '1'), ), ); if (query.result.isLoading) { return const Text(‘ಡΈࠐΈத’); } if (query.result.hasException) { return const Text(‘Τϥʔ'); } return Text(query.result.parsedData?.sample.name ?? ''); } } React Flutter