question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to import GraphQL queries?

See original GitHub issue

Hi, is it possible to import queries instead of write them directly into the HTTP request? something like:

import productsQuery from '~/queries/products.gql'
async asyncData({ $strapi }) {
	const products = await $strapi.graphql({ query: productsQuery })
	return { products }
}

instead of the documented method:

async asyncData({ $strapi }) {
	await $strapi.graphql({
		query: `	
			query {
				products {
        				...
      				}
    			}
		`
	});
}

In my case I’m getting HTTPError processTicksAndRejections

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
dubem-designcommented, Jan 6, 2021

@nonlinearcom No need for graphql-tag package, just use this example:

// apollo/mutations/files.js
export default {
  UPLOAD: `
  mutation upload($file: Upload!) {
    upload(file: $file) {
      name
      formats
    }
  }
`,
  DELETE: `
  mutation deleteFile($fileId: ID!) {
    deleteFile(input:{where:{id:$fileId}}) {
      file {
        name
        formats
      }
    }
  }
`,
}
// store/files.js
import mutations from '~/apollo/mutations/files.js'
 async DELETE({ commit }, { fileId, index }) {
      try {
        const data = await this.$strapi.graphql({
          query: mutations.DELETE,
          variables: { fileId },
        })...........

This works for me

1reaction
Atinuxcommented, Jan 21, 2021

Anyone feels like opening a PR for the docs to add the usage with gql on https://strapi.nuxtjs.org/strapi#graphqldata ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Loading queries with Webpack - Apollo GraphQL Docs
You can load GraphQL queries over .graphql files using Webpack. ... query CurrentUserForLayout { ... import { graphql } from '@apollo/react-hoc';.
Read more >
How to import GraphQL query? - Stack Overflow
To import GraphQL queries you need a specific loader. When you are using webpack you can for example use graphql-tag/loader from graphql-tag ...
Read more >
Getting Started with GraphQL, Part 1: Importing and Querying ...
Steps to get started · 1. GraphQL schema definition file · 2. Create a database using Fauna Shell and a provision a key...
Read more >
The graphql module exports a core subset of GraphQL ...
The graphql module exports a core subset of GraphQL functionality for creation of GraphQL type systems and servers. import { graphql } from...
Read more >
Loading .graphql Files - Create React App
import · query User { · user(id: 5) { · lastName ·...UserEntry1 · } · }
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found