Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can reuse query with variables and fragment in pageQuery #8046

Closed
sivagao opened this issue Sep 11, 2018 · 4 comments
Closed

How can reuse query with variables and fragment in pageQuery #8046

sivagao opened this issue Sep 11, 2018 · 4 comments
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@sivagao
Copy link

sivagao commented Sep 11, 2018

Summary

I want to reuse my fragment definition and query with variable in many types of page. how can i implement this.

for example:

export const articleNodeFields = graphql`
  fragment articleNodeFields on mongodbDatabaseArticles {
    id
    title
    source
    summary
    content
    info {
      author
      top_img
    }
  }
`

export const  querySourcedArticle= graphql`
query querySourcedArticle($source: String) {
    allMongodbDatabaseArticles(
      limit: 10
      filter: { info: { top_img: { ne: null } }, summary: { ne: null }, source: {eq: $source} }
      sort: { fields: id, order: DESC }
    ) {
      edges {
        node {
    ...articleNodeFields // HOW to use fragment here?
        }
      }
    }
}
`

and at page
```js
import {querySourcedArticle} from "file-from-above"

export const pageQuery = graphql`
  querySourcedArticle(source: "topic1") // HOW
`

## Relevant information

<!-- Provide as much useful information as you can -->

### Environment (if relevant)

<!--
  Required. Run `gatsby info --clipboard` in your gatsby project directory and paste its contents here.
  Not working? You may need to update your global gatsby-cli - `npm install -g gatsby-cli`
-->

### File contents (if changed)

`gatsby-config.js`: N/A <!-- Please use a code block or just leave it as is if wasn't changed -->
`package.json`: N/A <!-- Please use a code block or just leave it as is if wasn't changed -->
`gatsby-node.js`: N/A <!-- Please use a code block or just leave it as is if wasn't changed -->
`gatsby-browser.js`: N/A <!-- Please use a code block or just leave it as is if wasn't changed -->
`gatsby-ssr.js`: N/A <!-- Please use a code block or just leave it as is if wasn't changed -->
@kakadiadarpan kakadiadarpan added the type: question or discussion Issue discussing or asking a question about Gatsby label Sep 12, 2018
@kakadiadarpan
Copy link
Contributor

Hi @sivagao, please go through this documentation of querying with graphql - fragments. This should answer your question

(If you disagree, feel free to re-open 😄 and we can help you further)

@sivagao
Copy link
Author

sivagao commented Sep 13, 2018

@kakadiadarpan found how to reuse fragment. but i don't find any example for reusing query with variable(like function with parameters). newbie to gatsby's graphql

@kakadiadarpan
Copy link
Contributor

@sivagao currently, parameterized fragments are not supported by GraphQL. You can follow the original issue on GraphQL repo here - graphql/graphql-spec#204

@homemdasneves
Copy link

I guess I found a sort of hacky way to do this: #11205

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

3 participants