Giter Site home page Giter Site logo

File Upload about mercurius HOT 9 CLOSED

mercurius-js avatar mercurius-js commented on May 22, 2024
File Upload

from mercurius.

Comments (9)

mcollina avatar mcollina commented on May 22, 2024

I'm not sure what you are trying to achieve. Can you please describe it a bit more and include an example of something that is not working as expected? Include a sample request, possibly with got or similar.

(schema is missing in your example).

from mercurius.

artecoop avatar artecoop commented on May 22, 2024

Bella Matteo,
what I'm trying to achieve is file upload thru graphql.

This is the schema I use to define the upload.
Before all, import grapqhl-upload:
import { GraphQLUpload } from 'graphql-upload';

then

typeDefs: buildSchema(`
    scalar Upload
 `),
resolvers: {
    Upload: GraphQLUpload
}

And then when needed:

type Mutation {
    addFile(id: ID!, file: Upload!): Boolean 
}

In the resolvers, file is managed as standard file
const { filename, mimetype, createReadStream } = await file;

I'm migrating from apollo-server and worked well.

from mercurius.

mcollina avatar mcollina commented on May 22, 2024

I'm still not sure what you are trying to achieve. I'm probably missing some context regarding file uploads and graphql, as I always thought they were two very separate concepts (there is nothing special here to handle them).

Having an example including a sample request would be extremely helpful.

(I'm currently boarding a long haul flight, so I'll reply when I get a chance).

from mercurius.

artecoop avatar artecoop commented on May 22, 2024

Ok I digged a little bit more and every POST with multipart/form-data to the endpoint returns 400.
I'll make some more test and then I'll try to create a codesandbox or minimal repo for you

Have a nice flight!

from mercurius.

artecoop avatar artecoop commented on May 22, 2024

Hey @mcollina, I hope your trip was flawless.

here's a way to reproduce the problem:
add those dependencies to package.json

"fastify": "2.12.0",
"fastify-gql": "3.0.6",
"fastify-cors": "3.0.1",
"fastify-multipart": "1.0.5"

and then in a index.js:

import fastify = require('fastify');
import GQL = require('fastify-gql');
import cors = require('fastify-cors');
import multipart = require('fastify-multipart');
import  GraphQLUpload = require('graphql-upload');

const app = fastify();

app.register(cors, { origin: '*' });

app.register(multipart, { addToBody: true });

const schema = `
    scalar Upload
    type Mutation {
        add(x: Int, y: Upload): Int
    }
`;

const resolvers = {
    Upload: GraphQLUpload,
    Mutation: {
        add: async (_, { x, y }) => {
            console.log(x);
            console.log(y);

            return x;
        }
    }
};

app.register(GQL, {
    path: '/',
    graphiql: 'playground',
    schema,
    resolvers
});

const port = 4000;
app.listen(port, () => console.log(`🚀  Server ready at http://localhost:${port}`));

If you try to post a file

  async function addImage(event) {
        const ADDIMAGE = gql`
            mutation($file: Upload!) {
                add(x: 28, y: $file)
            }
        `;

        const file = event.target.files[0];
        console.log(file);
        await mutate(client, {
            mutation: ADDIMAGE,
            variables: {
                file
            }
        });
}

You'll get
Uncaught (in promise) Error: Network error: Response not successful: Received status code 400

from mercurius.

mcollina avatar mcollina commented on May 22, 2024

Your problem is that https://github.com/jaydenseric/graphql-upload/blob/master/readme.md does not support fastify.

from mercurius.

artecoop avatar artecoop commented on May 22, 2024

Well, this sucks. I have to roll back to apollo-server because file upload is a core feature for my app.
But I can try to spare some time to make fastify-gql compatible with graphql-upload. Any hints will be appreciated.
You can close this issue if you think.

from mercurius.

ericlewis avatar ericlewis commented on May 22, 2024

@mcollina seems resolved by #126

from mercurius.

mcollina avatar mcollina commented on May 22, 2024

Closing then!

from mercurius.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.