[GraphQL error]: Message: Unknown fragment
Asked Answered
R

1

8

I want to use some fragment:

import {gql} from "apollo-boost";
import "../fragments/cardFragments.graphql"

export const ADD_CARD = gql`
    mutation AddCard {
        createCard(input: {
            private: true,
            section: "school",
            createdBy: "api/users/1"
        }) {
            card {
                ...CardFields
            }
        }
    }
`;

export default {ADD_CARD}

cardFragments.graphql:

fragment CardFields on card {
    id
    private
    section
    createdBy {
        id
    }
}

Inside the console I get the error :

[GraphQL error]: Message: Unknown fragment "CardFields"., Location: [object Object], Path: undefined

Did I forgot something?

EDIT:

For the graphql fragmets to work I need to load it with webpack: Apollo docs

I did this with Webpack Encore:

.addLoader({
    test: /\.(graphql|gql)$/,
    exclude: /node_modules/,
    loader: 'graphql-tag/loader',
});

module.exports = Encore.getWebpackConfig();

Before I did this - I got an error for the not loadable .graphql extention inside of Webpack Encore.

Is there something I do not see about creating cutsom loader with Webpack Encore?

Rectrix answered 21/5, 2020 at 17:38 Comment(5)
It's really unclear what you expect import "../fragments/cardFragments.graphql" to do. A graphql file is not even valid JS code, so you must be relying on some build tool as well?Dews
@Bergi, most likely graphql-tag/loader for webpackRobbery
@JosephD. Is that implied by the apollo-client tag?Dews
@Dews yes. Apollo is a graphql client.Robbery
Ok, I use webpack encore with symfony and created a custom loader: with the description form the docs - before I did this I got an error that this file format can not be loaded. So now it gets loaded. Should I register that somewere in the new ApolloClient() function?Rectrix
G
7

You should import { CardFields } from "../fragments/cardFragments.js" and then use this fragment in your mutation like this:

import {gql} from "apollo-boost";
import { CARD_FEILDS } from "../fragments/cardFragments.js"

export const ADD_CARD = gql`
    mutation AddCard {
        createCard(input: {
            private: true,
            section: "school",
            createdBy: "api/users/1"
        }) {
            card {
                ...CardFields
            }
        }
    }
    ${CARD_FEILDS}
`;

export default {ADD_CARD}

In cardFragments.js:

import {gql} from "apollo-boost"
export const CARD_FEILDS = gql `
  fragment CardFields on card {
    id
    private
    section
    createdBy {
        id
    }
  }
`
Glazing answered 22/5, 2020 at 5:57 Comment(4)
Thank u for this suggestion. Nice to know tat its possible to solve it this way when the webpack loading configuration does not work at all. Do u solve it this way? Its not the recommended way isn't it ?Rectrix
@MichaelBrauner I think you should use .js file as much as possible rather than using .graphql file in React app. So you just need babel-loader to compile your file. I think it's better than use .graphql fileGlazing
Ok, I will do it this way. Thank you.Rectrix
Just so you know you do not need gql on your fragmentReally

© 2022 - 2024 — McMap. All rights reserved.