Implement graphql mutation to read the csv file that uploaded along mutation using…
- User
Implement graphql mutation to read the csv file that uploaded along mutation using
pothos-graphqlandfastify, give me a code only? - Assistant
const { GraphQLFile } = require('graphql-file-loader'); const { ApolloServer, gql } = require('apollo-server-fastify');
const uploadCSV = gql
mutation UploadCSV($csv: Upload!) { uploadCSV(csv: $csv) { id url } };const readCSV = gql` type CSVData { data: String }
codetype Query { readCSV(id: ID!): CSVData }`;
const csvResolver = { Query: { readCSV: async (_, { id }) => { const file = await fs.promises.readFile(id); const stream = fs.createReadStream(file); const parser = csv.parse({ headers: true });
codeconst data = []; return new Promise((resolve, reject) => { stream.pipe(parser).on('data', (row) => { data.push(row); }).on('end', () => { resolve({ data: JSON.stringify(data) }); }).on('error', (err) => { reject(err); }); }); }, },};
const server = new ApolloServer({ typeDefs: [readCSV, uploadCSV], resolvers: csvResolver, onReady: async () => { console.log('Server ready at http://localhost:3000/graphql'); }, });
server.listen().then(({ url }) => { console.log(
Server ready at ${url}); });
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.