How can I get my graphql query?

Hi, I’m new to datoCms and I checked the documentations for connecting the graphql to my Vue cli project but when I open the website on my local host I get this error and I don’t know how to solve it ?

vue.runtime.esm.js?c320:4573 [Vue warn]: Error in mounted hook (Promise/async): "[{"message":"Parse error on \"course\" (IDENTIFIER) at [2, 9]","locations":[{"line":2,"column":9}]}]"

this is my api and my code

import axios from 'axios'

export async function request({ query, variables, preview }) {
    const endpoint = preview
        ? `https://graphql.datocms.com/preview`
        : `https://graphql.datocms.com/`

    const { data } = await axios.post(
        endpoint,
        {
            query,
            variables
        },
        {
            headers: {
                Authorization:
                    `Bearer ${process.env.VUE_APP_CMS_DATOCMS_API_TOKEN}`
            }
        }
    )

    if (data.errors) {
        throw JSON.stringify(data.errors);
    }

    return data.data;
}
<template>
  <div>
    <div>{{ data }}</div>
  </div>
</template>
<script>
import { request } from "./datocms";
export default {
  name: "App",
  data() {
    return {
      data: null
    };
  },
  async mounted() {
    this.data = await request({
      query: `
        course {
    id
    name
    courseHeader {
      ... on CourseHeaderRecord {
        smallTitle
        bigTitle
        buttonText
        description
      }
    }
  }
      `
    });
  }
};
</script>

Hello @zagros and welcome to the community!

If the query variable contains the exact query on your first screenshot, the problem is that it should be wrapped arround a query declaration:

query MyQuery {
  course {...}
}

Change the variable and let me know if it works!

Thank you! everything now working correctly! I have access to the API from local :smiling_face:

1 Like