简体   繁体   English

[GraphQL 错误]:消息:未知片段

[英][GraphQL error]: Message: Unknown fragment

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: 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 [GraphQL 错误]:消息:未知片段“CardFields”。,位置:[object Object],路径:未定义

Did I forgot something?我忘了什么吗?

EDIT:编辑:

For the graphql fragmets to work I need to load it with webpack: Apollo docs为了使 graphql 片段工作,我需要用 webpack 加载它: Apollo docs

I did this with Webpack Encore :我用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 .在我这样做之前 - 我在Webpack Encore内部遇到了 not loadable.graphql 扩展的错误。

Is there something I do not see about creating cutsom loader with Webpack Encore ?关于使用Webpack Encore创建 cutsom loader 有什么我看不到的吗?

You should import { CardFields } from "../fragments/cardFragments.js" and then use this fragment in your mutation like this:您应该import { CardFields } from "../fragments/cardFragments.js" ,然后在您的突变中使用此片段,如下所示:

 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:在 cardFragments.js 中:

 import {gql} from "apollo-boost" export const CARD_FEILDS = gql ` fragment CardFields on card { id private section createdBy { id } } `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM