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

graphql-relay-result

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-relay-result

Relay Result is a tiny layer on top of Relay's libraries to add type-safe payloads for GraphQL mutations.

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
increased by200%
Maintainers
0
Weekly downloads
 
Created
Source

Relay Result

Relay Result is a tiny layer on top of Relay's libraries to add type-safe payloads for GraphQL mutations.

Example of the generated types:

union UserLoginPayload = UserLoginSuccess | UserLoginError

type UserLoginSuccess {
  user: User!
  clientMutationId: String
}

type UserLoginError {
  error: String!
  clientMutationId: String
}

input UserLoginInput {
  """
  Email of the user attempting to log in
  """
  email: String!

  """
  Password of the user attempting to log in
  """
  password: String!
  clientMutationId: String
}

Installation

Install graphql-relay-result in your backend:

npm install --save graphql graphql-relay graphql-relay-result
# Or Yarn
yarn add graphql graphql-relay graphql-relay-result
# Or PNPM
pnpm add graphql graphql-relay graphql-relay-result
# Or Bun
bun add graphql graphql-relay graphql-relay-result

Install react-relay-result in your frontend:

npm install --save react-relay react-relay-result
# Or Yarn
yarn add react-relay react-relay-result
# Or PNPM
pnpm add react-relay react-relay-result
# Or Bun
bun add react-relay react-relay-result

Usage

import { GraphQLString, GraphQLInt, GraphQLNonNull } from 'graphql';
import { mutationWithResult } from 'graphql-relay-result';

type UserLoginInput = {
  email: string;
  password: number;
};

type UserLoginSuccess = {
  userId: string;
};

type UserLoginError = {
  error: string;
};

export const UserLogin = mutationWithResult<
  UserLoginInput,
  UserLoginSuccess,
  UserLoginError,
  __YourContext,
  __YourCustomExtensions
>({
  name: 'UserLogin',
  inputFields: {
    email: {
      type: new GrapqhQLNonNull(GraphQLString),
    },
    password: {
      type: new GraphqlNonNull(GraphQLInt),
    },
  },
  mutateAndGetPayload: ({ email, password }) => {
    const userLoginResult = await userLogin({ email, password });

    if (!userLoginResult.success) {
      return {
        success: false,
        error: userLoginResult.error,
      };
    }

    const { user } = userLoginResult;

    return {
      userId: user.id,
    };
  },
  successFields: {
    user: {
      type: new GraphQLNonNull(UserType),
      resolve: (result) => userLoad(result.userId),
    },
  },
  errorFields: {
    error: {
      type: GraphQLString,
      resolve: (result) => result.error,
    },
  },
});

In the frontend:

// UserLoginMutation.tsx
import { graphql } from 'react-relay';

export const UserLogin = graphql`
  mutation UserLoginMutation($input: UserLoginInput!) {
    userLogin(input: $input) {
      __typename

      ... on UserLoginSuccess {
        user {
          id
          name
        }
      }

      ... on UserLoginError {
        error
      }
    }
  }
`;

// LoginForm.tsx
import { useMutationResult } from 'react-relay-result';

import { UserLogin } from './UserLoginMutation';
import type { UserLoginMutation } from './__generated__/UserLoginMutation.graphql';

const LoginForm = () => {
  const [userLogin, isPending] = useMutationResult<UserLoginMutation>({
    name: 'UserLogin',
    mutation: UserLogin,
    onSuccess: ({ user }) => {
      console.log(user.id);
    },
    onError: ({ error }) => {
      console.log(error);
    },
  });

  return (
    // ...
  )
};

export default LoginForm;

License

Relay Result is under the MIT License.

Keywords

FAQs

Package last updated on 24 Aug 2024

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