我想使用一些片段:

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
    }
}

在控制台内,我得到了错误:



我忘了什么吗

编辑:

为了使graphql fragmets正常工作,我需要使用webpack加载它:Apollo docs

我用Webpack Encore做到了:
.addLoader({
    test: /\.(graphql|gql)$/,
    exclude: /node_modules/,
    loader: 'graphql-tag/loader',
});

module.exports = Encore.getWebpackConfig();

在执行此操作之前-Webpack Encore内部无法加载的.graphql扩展出现错误。

关于使用Webpack Encore创建cutsom加载器,我没有看到什么吗?

最佳答案

您应该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}


在cardFragments.js中:

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

关于javascript - [GraphQL错误]:消息:未知片段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61940478/

10-15 04:36