首页 > 解决方案 > Connecting Strapi to NextJS/Apollo Client GraphQL -- Variable "$input" of required type 'XXX' was not provided

问题描述

Forgive me, this is very basic, but I haven't worked with GraphQL in a year or so and I'm missing the obvious. I'm trying to connect a Strapi backend to a NextJS/Apollo Client front-end using GraphQL and the basic syntax is eluding me. (This is a 'proof of concept' for a project we're working on, just to see if we can use Strapi, NextJS, and a rich text editor for our use-case)

Strapi has provided me with the createPost endpoint

enter image description here

To which, as far as I can tell, I need to send an object with a field data which contains my text. I'm using TinyMCE's editor to edit the text and using apollo hooks to get the mutation

const TheEditor = () => {
    const [addPost, {data}] = useMutation(ADD_POST);
    const [theText, setText] = useState('')

    const handleEditorChange = (content, editor) => {
        console.log('Content was updated:', content);
        setText(content)
    }

    return <>
        <Editor apiKey='4564654765476547654'
                initialValue="<p>This is the initial content of the editor</p>"
                init={{
                    height: 500,
                    menubar: false,
                    plugins: [
                        'advlist autolink lists link image charmap print preview anchor',
                        'searchreplace visualblocks code fullscreen',
                        'insertdatetimse media table paste code help wordcount'
                    ],
                    toolbar:
                        'undo redo | formatselect | bold italic backcolor | \
                        alignleft aligncenter alignright alignjustify | \
                        bullist numlist outdent indent | removeformat | help'
                }}
                onEditorChange={handleEditorChange}
        />
        <button onClick={async e => {
            e.preventDefault();
            await addPost({variables: {data:theText}});
        }}>ENTER
        </button>

    </>
}

export default withApollo(TheEditor);

My mutation is

const ADD_POST = gql`
      mutation ADD_POST($input: createPostInput!) {
        createPost(input: $input) {
          post {
          text 
          }
        }
      }
`;

But I get the error

enter image description here

Clearly the issue is I need to go back and learn the basics of GraphQL syntax again. In the meantime I'm hoping someone can point out the (egregious) errors of my ways. I have been following along with the Apollo tutorial here, but I can't seem to make the leap from their code to mine. Any help much appreciated.

标签: graphqltinymcereact-apolloapollo-clientstrapi

解决方案


你的变量名是输入而不是数据

<button onClick={async e => {
                e.preventDefault();
                await addPost({variables: { input:theText }});
            }}>ENTER
</button>

推荐阅读