简体   繁体   中英

make more modular graphQL project

I would like to start by writing an environment which unfortunately I had to use

windows 10 / 64 bit's
Visual-Studio-Code

I'm new in GraphQL technology that why I have this kinde od questions.

I do not understand why when all the resolvers functions are in one file, the whole project works great..

unfortunately when it starts to take more care of the structure, everything starts to making new error

As belowe I have this type of error, when I try to blast off node server

 (function (exports, require, module, __filename, __dirname) {   c ^ SyntaxError: Invalid or unexpected token at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:616:28) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object.<anonymous> (C:\\Users\\Weronika\\Documents\\michal\\first-graphgl-server\\src\\index.js:5:18) 

Here I am posting the project structure

--database/
--src/
----generated/
----resolvers/
------Mutation.js
------AuthPayload.js
----index.js
--package.json
--.graphqlconfig.yml
schema.graphql
utils.js

Moreover, I post my dependencies belowe:

{
  "name": "first-graphql-server",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "graphql-yoga": "^1.14.12",
    "jsonwebtoken": "^8.3.0",
    "prisma-binding": "^2.1.1"
  }
}

And, belowe, Mutation.js file, which is the problem

const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const { APP_SECRET, getUserId } = require('../utils')


async function signup(parent, args, context, info) {

  const password = await bcrypt.hash(args.password, 10)

  const user = await context.db.mutation.createUser({
    data: {...args, password},
  }, `{ id }`)

  const token = jwt.sign({ userId: user.id }, APP_SECRET)

  return {
    token,
    user,
  }
}

async function login(parent, args, context, info) {

  const user = await context.db.query.user({ where: { email: args.email } }, `{ id password }`)

  if (!user) {
    throw new Error('No such user found')
  }

  const valid = await bcrypt.compare(args.password, user.password)

  if (!valid) throw new Error('Invalid password')

  const token = jwt.sign({ userId: user.id }, APP_SECRET)

  return {
    token,
    user,
  }
}

function post(parent, args, context, info) {
  const userId = getUserId(context)
  return context.db.mutation.createLink(
    {
      data: {
        url: args.url,
        description: args.description,
        postedBy: { connect: { id: userId } },
      },
    },
    info,
  )
}

module.exports = {
  signup,
  login,
  post,
}

And the last file wiche I have to copy to the post, but the most important one, index.js file

const { GraphQLServer } = require('graphql-yoga');
const { Prisma } = require('prisma-binding');


const Mutation = require('./resolvers/Mutation')
const AuthPayload = require('./resolvers/AuthPayload')

function feed(parent, args, context, info) {
  return context.db.query.links({}, info)
}

const Query = {
  feed,
}

const resolvers = { 
  Query,
  Mutation,
  AuthPayload
}

const server = new GraphQLServer({
  typeDefs: './src/schema.graphql',
  resolvers,
  context: req => ({
    ...req,
    db: new Prisma({
      typeDefs: 'src/generated/prisma.graphql',
      endpoint: 'https://eu1.prisma.sh/mjaracz-b561d8/example/dev',
      secret: 'mysecret123',
      debug: true,
    }),
  }),
})
server.start(() => console.log(`Server is running on the https://localhost:4000`))

Please let me know if any of you have the same problem?

Keep calm... and care for good code quality,

I hope this phrase is true to me and learning project, good day everyone :)

我猜想,您在Mutation.js文件或某些奇怪的空格中编码错误。

Your error has nothing to do with the implementation itself. There is an invalid character in one of your files:

(function (exports, require, module, __filename, __dirname) { c

Notice the characters.

Remove them to solve this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM