Rumah  >  Soal Jawab  >  teks badan

Vue Apollo memuat naik fail menyebabkan Node ranap dan saiz tindanan panggilan melebihi had maksimum _openReadFs

Saya cuba menyediakan bahagian hadapan muat naik fail graphQl menggunakan Apollo-boost-upload. Kod bahagian belakang adalah berdasarkan pautan ini https://dev.to/dnature/handling-file-uploads-with-apollo-server-2-0-14n7. Selepas menambah baris berikut dalam fail server.js, titik putus parser kini dicapai

const { apolloUploadExpress } = require("apollo-upload-server");

app.use(apolloUploadExpress({ maxFileSize: 1000000000, maxFiles: 10 }));

Selepas mengubah suai struktur jenis muat naik

scalar Upload

Ini ialah komponen Vue

 <input
    type="file"
    style="display:none"
    ref="fileInput"
    accept="image/*"
    @change="upload"
>

//Upload method 
  upload({ target: { files = [] } }) {
        if (!files.length) {
          return;
        }
        this.logoImage = files[0];
      },

//Dispatching action from vue component

this.$store.dispatch("uploadLogo", { image: this.logoImage });

//Vuex action
const uploadLogo = async (context, payload) => {
  context.commit("setLoading", true);
  try {
    const { data } = await apolloClient.mutate({
      mutation: UPLOAD_LOGO,
      variables: {file: payload.image},
      context: {
        hasUpload: true,
      },
    });
    context.commit("setLoading", false);
    console.log("Logo:", data.uploadLogo);
  } catch (error) {
    context.commit("setLoading", false);
    console.log(error);
  }
};

//Mutation
export const UPLOAD_LOGO = gql`
  mutation uploadLogo($file: Upload!) {
    uploadLogo(file: $file) {
      _id
      path
      filename
      mimetype
      user {
        _id
      }
    }
  }
`;


// Apolloclient config on main.js

import ApolloClient from "apollo-boost-upload";

import { InMemoryCache } from "apollo-boost";
import VueApollo from "vue-apollo";


// Set up Apollo Client
export const defaultClient = new ApolloClient({
  uri: "http://localhost:4000/graphql",
  cache: new InMemoryCache({
    addTypename: false,
  }),
 
  fetchOptions: {
    credentials: "include",
  },
  request: (operation) => {
    // if no token in local storage, add it
    if (!localStorage.someToken) {
      localStorage.setItem("someToken", "");
    }
    // operation adds the token to authorizatrion header, which is sent o backend
    operation.setContext({
      headers: {
        authorization: "Bearer " + localStorage.getItem("someToken"),
      },
    });
  },
  onError: ({ graphQLErrors, networkError }) => {
    if (networkError) {
      console.log("[networkError]", networkError);
    }
    if (graphQLErrors) {
      for (const error of graphQLErrors) {
        console.dir(error);
        if (error.name === "AuthenticationError") {
          // set auth errir in state
          store.commit("setError", error);
          // signout user to clear error
          store.dispatch("signUserOut");
        }
      }
    }
  },
});

Berikut ialah typedef yang dikemas kini untuk bahagian belakang (kod lama diulas) jika ini membantu mengenal pasti masalah

const logoUploadTypeDefs = gql`
  type File {
    _id: ID!
    path: String!
    filename: String!
    mimetype: String!
    encoding: String!
    user: User
  }

  # input Upload {
  #   name: String!
  #   type: String!
  #   size: Int!
  #   path: String!
  # }

  scalar Upload

  type Mutation {
    uploadLogo(file: Upload!): File
  }
  type Query {
    info: String
    logo: File!
  }
`;

Kini, aplikasi Node ranap dengan log berikut

P粉331849987P粉331849987205 hari yang lalu379

membalas semua(1)saya akan balas

  • P粉276064178

    P粉2760641782024-03-28 09:12:26

    Saya terpaksa menukar "apollo-upload-server" kepada "graphql-upload"

    Perubahan 1:

    Ulas "apollo-upload-server" dan gunakan "graphql-upload"

    // const { apolloUploadExpress } = require("apollo-upload-server");]
    const {
      graphqlUploadExpress, // A Koa implementation is also exported.
    } = require("graphql-upload");

    Dalam middleware, ini digunakan

    Tukar 2:

    app.use(graphqlUploadExpress());
    await apolloServer.start();

    Ganti kod lama

    app.use(apolloUploadExpress());// Not to be used
    await apolloServer.start();

    Juga, dalam parser, saya menambah ini

    Tukar 3:

    Import Muat Naik dari graphql-upload dalam fail parser

    const { GraphQLUpload } = require("graphql-upload");
    
    ....
    ....
    
    const resolvers = {
      // This maps the `Upload` scalar to the implementation provided
      // by the `graphql-upload` package.
      Upload: GraphQLUpload,
      Query: {
          ....
       }
      Mutations: {
          ....
      }
    
    }

    Lihat dokumentasi Apollo untuk butiran lanjut. Ini membetulkan ranap nod dengan ralat "_openReadFs melebihi saiz tindanan panggilan maksimum..."

    balas
    0
  • Batalbalas