search

Home  >  Q&A  >  body text

Import the GraphQL file in nuxt.js.

<p>So I'm importing the schema generated by postgraphile, but it's not loading correctly. </p><p>This is the type of output: </p><p><br /></p> <pre class="brush:php;toolbar:false;">definitions: Array(44) [ {…}, {…}, {…}, … ] kind: "Document" loc: Object { start: 0, end: 26188, source: {…} }</pre> <p>I have tried various code variations to load my query allUsers. </p> <pre class="brush:php;toolbar:false;"><script setup> import Schema from '@/graphql/schema.gql' console.log('Query', Schema.valueOf('allUsers')) const { data } = await useAsyncQuery(Query) </script></pre> <p><br /></p>
P粉754473468P粉754473468493 days ago554

reply all(1)I'll reply

  • P粉807471604

    P粉8074716042023-07-29 00:04:48

    The valueOf method is not suitable for this purpose. GraphQL schema documents do not have direct access to their queries, mutations, or subscriptions via key-value access methods. ,

    First, make sure you have installed the graphql-tag package in your project:

    npm install graphql-ta

    Define your query in schema.gql:


    query AllUsers {
      allUsers {
        nodes {
          id
          name
          # other fields...
        }
      }
    }

    Import and use AllUsers query in the component:

    <script setup>
    import { useQuery } from "@vue/apollo-composable";
    import { loader } from "graphql.macro";
    
    const Schema = loader('@/graphql/schema.gql');
    
    const { result } = useQuery({
      query: Schema.AllUsers
    });
    
    // Here, 'result' will contain your fetched data when available.
    </script>

    Hope it’s useful

    reply
    0
  • Cancelreply