Пример #1
0
async def flexible_api_handler(service, action_type, payload, props, **kwds):
    """
        This query handler builds the dynamic picture of availible services.
    """
    # if the action represents a new service
    if action_type == intialize_service_action():
        # the treat the payload like json if its a string
        model = json.loads(payload) if isinstance(payload, str) else payload

        # the list of known models
        models = service._external_service_data['models']
        # the list of known connections
        connections = service._external_service_data['connections']
        # the list of known mutations
        mutations = service._external_service_data['mutations']

        # if the model is a connection
        if 'connection' in model:
            # if we haven't seen the connection before
            if not [
                    conn
                    for conn in connections if conn['name'] == model['name']
            ]:
                # add it to the list
                connections.append(model)

        # or if there are registered fields
        elif 'fields' in model and not [
                mod for mod in models if mod['name'] == model['name']
        ]:
            # add it to the model list
            models.append(model)

        # the service could provide mutations as well as affect the topology
        if 'mutations' in model:
            # go over each mutation announce
            for mutation in model['mutations']:
                # if there isn't a mutation by the same name in the local cache
                if not [
                        mut
                        for mut in mutations if mut['name'] == mutation['name']
                ]:
                    # add it to the local cache
                    mutations.append(mutation)

        # if there are models
        if models:
            # create a new schema corresponding to the models and connections
            service.schema = generate_api_schema(
                models=models,
                connections=connections,
                mutations=mutations,
            )
Пример #2
0
    def test_generate_api_schema(self):
        # create mock summaries
        model_summary = MockModelService()().summarize()
        connection_summary = MockConnectionService()().summarize()
        # create the graphql schema
        schema = generate_api_schema([model_summary], [connection_summary])

        # grab the object type corresponding to our mock
        field = [field for field in schema.query._meta.local_fields if field.default_name == 'testModel']
        # make sure such an object type exists
        assert field, (
            "No object type added to api schema"
        )
Пример #3
0
    def test_generate_api_schema(self):
        # create mock summaries
        model_summary = MockModelService()().summarize()
        connection_summary = MockConnectionService()().summarize()
        # create the graphql schema
        schema = generate_api_schema([model_summary], [connection_summary])

        # grab the object type corresponding to our mock
        field = [
            field for field in schema.query._meta.local_fields
            if field.default_name == 'testModel'
        ]
        # make sure such an object type exists
        assert field, ("No object type added to api schema")
Пример #4
0
async def flexible_api_handler(service, action_type, payload, props, **kwds):
    """
        This query handler builds the dynamic picture of availible services.
    """
    # if the action represents a new service
    if action_type == intialize_service_action():
        # the treat the payload like json if its a string
        model = json.loads(payload) if isinstance(payload, str) else payload

        # the list of known models
        models = service._external_service_data['models']
        # the list of known connections
        connections = service._external_service_data['connections']
        # the list of known mutations
        mutations = service._external_service_data['mutations']

        # if the model is a connection
        if 'connection' in model:
            # if we haven't seen the connection before
            if not [conn for conn in connections if conn['name'] == model['name']]:
                # add it to the list
                connections.append(model)

        # or if there are registered fields
        elif 'fields' in model and not [mod for mod in models if mod['name'] == model['name']]:
            # add it to the model list
            models.append(model)

        # the service could provide mutations as well as affect the topology
        if 'mutations' in model:
            # go over each mutation announce
            for mutation in model['mutations']:
                # if there isn't a mutation by the same name in the local cache
                if not [mut for mut in mutations if mut['name'] == mutation['name']]:
                    # add it to the local cache
                    mutations.append(mutation)

        # if there are models
        if models:
            # create a new schema corresponding to the models and connections
            service.schema = generate_api_schema(
                models=models,
                connections=connections,
                mutations=mutations,
            )
Пример #5
0
    def test_generate_api_schema_with_mutation(self):
        model_service = MockModelService()()
        # create mock summaries
        model_summary = model_service.summarize()
        mutation_summary = summarize_crud_mutation(model=model_service, method='create')

        # create the graphql schema
        schema = generate_api_schema(
            models=[model_summary],
            mutations=[mutation_summary]
        )

        # the list of mutations in the schema
        schema_mutations = [field.default_name for field in schema.mutation._meta.local_fields]

        # make sure the schema has the correct mutation list
        assert schema_mutations == ['createTestModel'], (
            "Generated schema did not have the correct mutations"
        )
Пример #6
0
    def test_generate_api_schema_with_mutation(self):
        model_service = MockModelService()()
        # create mock summaries
        model_summary = model_service.summarize()
        mutation_summary = summarize_crud_mutation(model=model_service,
                                                   method='create')

        # create the graphql schema
        schema = generate_api_schema(models=[model_summary],
                                     mutations=[mutation_summary])

        # the list of mutations in the schema
        schema_mutations = [
            field.default_name for field in schema.mutation._meta.local_fields
        ]

        # make sure the schema has the correct mutation list
        assert schema_mutations == [
            'createTestModel'
        ], ("Generated schema did not have the correct mutations")