Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@deboxsoft/users-graphql-api

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@deboxsoft/users-graphql-api

Server side GraphQL transport for users

  • 1.2.1-3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17
increased by466.67%
Maintainers
1
Weekly downloads
 
Created
Source

@deboxsoft/users-graphql-api

Schema, Resolvers and Utils for GraphQL server with JSUsers

This package does not requires any network interface / express in order to combine with your GraphQL - it's just a collection of GraphQL schema, resolvers and utils!

How to use this package?

This package exports GraphQL schema and GraphQL resolvers, which you can extend with your existing GraphQL schema server.

Start by installing it from NPM / Yarn:

yarn add @deboxsoft/users-server @deboxsoft/users-graphql-api

This package does not create a transport or anything else, only schema and string and resolvers as object.

Start by configuring your UsersServer as you wish. For example, using MongoDB:

import mongoose from "mongoose";
import UsersServer from "@deboxsoft/users-server";
import UsersPassword from "@deboxsoft/users-password";
import MongoDBInterface from "@deboxsoft/users-mongo";

const db = mongoose.connection;

const password = new UsersPassword();

const usersServer = new UsersServer(
  {
    db: new MongoDBInterface(db),
    tokenSecret: "SECRET"
  },
  {
    password
  }
);

Next, import UsersModule from this package, and run it with your UsersServer:

import { UsersModule } from "@deboxsoft/users-graphql-api";

const usersGraphQL = UsersModule.forRoot({
  usersServer
});

Now, add usersGraphQL.typeDefs to your schema definition (just before using it with makeExecutableSchema), and merge your resolvers object with usersGraphQL.resolvers by using @graphql-tools/epoxy, for example:

import { makeExecutableSchema } from "@graphql-tools/schema";
import { mergeGraphQLSchemas, mergeResolvers } from "@graphql-tools/epoxy";

const typeDefs = [
  `
  type Query {
    myQuery: String
  }

  type Mutation {
    myMutation: String
  }

  schema {
      query: Query,
      mutation: Mutation
  }
  `,
  usersGraphQL.typeDefs
];

let myResolvers = {
  Query: {
    myQuery: () => "Hello"
  },
  Mutation: {
    myMutation: () => "Hello"
  }
};

const schema = makeExecutableSchema({
  resolvers: mergeResolvers([usersGraphQL.resolvers, myResolvers]),
  typeDefs: mergeGraphQLSchemas([typeDefs])
});

The last step is to extend your graphqlExpress with a context middleware, that extracts the authentication token from the HTTP request, so UsersServer will automatically validate it:

app.use(
  GRAPHQL_ROUTE,
  bodyParser.json(),
  graphqlExpress((request) => {
    return {
      context: {
        ...usersGraphQL(request)
        // your context
      },
      schema
    };
  })
);

Authenticating Resolvers

You can authenticate your own resolvers with JSUsers authentication flow, by using authenticated method from this package.

This method composer also extends context with the current authenticated user!

This is an example for a protected mutation:

import UsersServer from "@users/deboxsoft-server";
import { authenticated } from "@users/deboxsoft-graphql-api";

export const resolver = {
  Mutation: {
    updateUserProfile: authenticated((rootValue, args, context) => {
      // Write your resolver here
      // context.user - the current authenticated user!
    })
  }
};

Customization

This package allow you to customize the GraphQL schema and it's resolvers.

For example, some application main query called MyQuery or RootQuery instead of query, so you can customize the name, without modifying you application's schema.

These are the available customizations:

  • rootQueryName (string) - The name of the root query, default: Query.
  • rootMutationName (string) - The name of the root mutation, default: Mutation.
  • extend (boolean) - whether to add extend before the root type declaration, default: true.
  • withSchemaDefinition (boolean): whether to add schema { ... } declaration to the generation schema, default: false.

Pass a second object to createUsersGraphQL, for example:

Another possible customization is to modify the name of the authentication header, use it with usersContext (the default is Authorization):

const myCustomGraphQLUsers = UsersModule.forRoot({
  usersServer,
  rootQueryName: "RootQuery",
  rootMutationName: "RootMutation",
  headerName: "MyCustomHeader"
});

Extending User

To extend User object with custom fields and logic, add your own GraphQL type definition of User with the prefix of extend, and add your fields:

extend type User {
  firstName: String
  lastName: String
}

And also implement a regular resolver, for the fields you added:

const UserResolver = {
  firstName: () => "Dotan",
  lastName: () => "Simha"
};

Extending User during password creation

To extend the user object during the user creation you need to extend the CreateUserInput type and add your fields:

extend input CreateUserInput {
  profile: CreateUserProfileInput!
}

input CreateUserProfileInput {
  firstName: String!
  lastName: String!
}

The user will be saved in the db with the profile key set.

FAQs

Package last updated on 18 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc