Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare params before action calling #98

Merged
merged 3 commits into from
Jul 20, 2021
Merged

Prepare params before action calling #98

merged 3 commits into from
Jul 20, 2021

Conversation

icebob
Copy link
Member

@icebob icebob commented Jul 19, 2021

It can be used to prepare the incoming params (e.g. unwrapping the input) before calling the action.

Example
Define the prepareContextParams method.

Signature: prepareContextParams(params, actionName, context)

// api.service.js
module.exports = {
    name: "api",

    mixins: [
        // Gateway
        ApiGateway,

        // GraphQL Apollo Server
        ApolloService({}),
    ],
    methods: {
        prepareContextParams(params, actionName) {
            if (actionName.endsWith(".replace" && params.input) {
                return params.input;
            }
            return params;
        },
    },
});
//greeter.service.js
module.exports = {
    name: "greeter",

    actions: {
        replace: {
            graphql: {
                input: `input GreeterInput {
                    name: String!
                }`,
                mutation: "replace(input: GreeterInput!): String",
            },
            handler(ctx) {
                return `Hello ${ctx.params.name}`; // no need `input`
            },
        },
    }
}

Copy link
Member

@shawnmcknight shawnmcknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@icebob icebob merged commit ba289c1 into master Jul 20, 2021
@icebob icebob deleted the prepare-params branch July 20, 2021 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants